Linux scripting if exist

How to check if a file exists in a shell script

I’d like to write a shell script which checks if a certain file, archived_sensor_data.json , exists, and if so, deletes it. Following http://www.cyberciti.biz/tips/find-out-if-file-exists-with-conditional-expressions.html, I’ve tried the following:

[-e archived_sensor_data.json] && rm archived_sensor_data.json 

when I try to run the resulting test_controller script using the ./test_controller command. What is wrong with the code?

You must set one or more whitespace between opening square bracket «[» and option «-e» same as between filename and closing square bracket «]»

7 Answers 7

You’re missing a required space between the bracket and -e :

#!/bin/bash if [ -e x.txt ] then echo "ok" else echo "nok" fi 

I finally added two blank spaces, one after the opening square bracket and one before the closing one: [ -e archived_sensor_data.json ] && rm archived_sensor_data.json . The script seems to work now.

The main difference here is the fact that you are using «bash» scripting instead of «shell» scripting. Notice that the first line that you have added was #!/bin/bash, so you are telling the machine to use «bash» instead of sh. Because sh doesn’t recognize that argument «-e»

Here is an alternative method using ls :

(ls x.txt && echo yes) || echo no 

If you want to hide any output from ls so you only see yes or no, redirect stdout and stderr to /dev/null :

(ls x.txt >> /dev/null 2>&1 && echo yes) || echo no 

This code means: «if ls is successful, there is such file, otherwise, there is none». If ls failed, it does not mean that file is missing. It might be some other error. For example, create file in directory owned by root and try to do ls under regular user. It will fail with Permission denied , which is not equivalent that file does not exist.

The backdrop to my solution recommendation is the story of a friend who, well into the second week of his first job, wiped half a build-server clean. So the basic task is to figure out if a file exists, and if so, let’s delete it. But there are a few treacherous rapids on this river:

  • Everything is a file.
  • Scripts have real power only if they solve general tasks
  • To be general, we use variables
  • We often use -f force in scripts to avoid manual intervention
  • And also love -r recursive to make sure we create, copy and destroy in a timely fashion.
Читайте также:  Исходный код kali linux

Consider the following scenario:

We have the file we want to delete: filesexists.json

This filename is stored in a variable

:~/Documents/thisfolderexists filevariable="filesexists.json" 

We also hava a path variable to make things really flexible

:~/Documents/thisfolderexists pathtofile=".." :~/Documents/thisfolderexists ls $pathtofile filesexists.json history20170728 SE-Data-API.pem thisfolderexists 

So let’s see if -e does what it is supposed to. Does the files exist?

:~/Documents/thisfolderexists [ -e $pathtofile/$filevariable ]; echo $? 0 

However, what would happen, if the file variable got accidentally be evaluated to nuffin’

:~/Documents/thisfolderexists filevariable="" :~/Documents/thisfolderexists [ -e $pathtofile/$filevariable ]; echo $? 0 

What? It is supposed to return with an error. And this is the beginning of the story how that entire folder got deleted by accident

An alternative could be to test specifically for what we understand to be a ‘file’

:~/Documents/thisfolderexists filevariable="filesexists.json" :~/Documents/thisfolderexists test -f $pathtofile/$filevariable; echo $? 0 
:~/Documents/thisfolderexists filevariable="" :~/Documents/thisfolderexists test -f $pathtofile/$filevariable; echo $? 1 

So this is not a file and maybe, we do not want to delete that entire directory

man test has the following to say:

-b FILE FILE exists and is block special -c FILE FILE exists and is character special -d FILE FILE exists and is a directory -e FILE FILE exists -f FILE FILE exists and is a regular file . -h FILE FILE exists and is a symbolic link (same as -L) 

Источник

Check if file exists [BASH]

Any ideas please? I will be glad for any help. P.S. I wish I could show the entire file without the risk of being fired from school for having a duplicate. If there is a private method of communication I will happily oblige. My mistake. Fas forcing a binary file into a wrong place. Thanks for everyone’s help.

4 Answers 4

Little trick to debugging problems like this. Add these lines to the top of your script:

The set -xv will print out each line before it is executed, and then the line once the shell interpolates variables, etc. The $PS4 is the prompt used by set -xv . This will print the line number of the shell script as it executes. You’ll be able to follow what is going on and where you may have problems.

Here’s an example of a test script:

#! /bin/bash export PS4="\$LINENO: " set -xv FILE1="$" # Line 6 if [ ! -e "$FILE1" ] # Line 7 then echo "requested file doesn't exist" >&2 exit 1 else echo "Found File $FILE1" # Line 12 fi 

And here’s what I get when I run it:

$ ./test.sh .profile FILE1="$" 6: FILE1=.profile if [ ! -e "$FILE1" ] then echo "requested file doesn't exist" >&2 exit 1 else echo "Found File $FILE1" fi 7: [ ! -e .profile ] 12: echo 'Found File .profile' Found File .profile 

Here, I can see that I set $FILE1 to .profile , and that my script understood that $ . The best thing about this is that it works on all shells down to the original Bourne shell. That means if you aren’t running Bash as you think you might be, you’ll see where your script is failing, and maybe fix the issue.

Читайте также:  Habr linux для начинающих

I suspect you might not be running your script in Bash. Did you put #! /bin/bash on the top?

script.sh [-g] [-p] [-r FUNCTION_ID|-d FUNCTION_ID] FILE

You may want to use getopts to parse your parameters:

#! /bin/bash USAGE=" Usage: script.sh [-g] [-p] [-r FUNCTION_ID|-d FUNCTION_ID] FILE " while getopts gpr:d: option do case $option in g) g_opt=1;; p) p_opt=1;; r) rfunction_id="$OPTARG";; d) dfunction_id="$OPTARG";; [?]) echo "Invalid Usage" 1>&2 echo "$USAGE" 1>&2 exit 2 ;; esac done if [[ -n $rfunction_id && -n $dfunction_id ]] then echo "Invalid Usage: You can't specify both -r and -d" 1>&2 echo "$USAGE" >2& exit 2 fi shift $(($OPTIND - 1)) [[ -n $g_opt ]] && echo "-g was set" [[ -n $p_opt ]] && echo "-p was set" [[ -n $rfunction_id ]] && echo "-r was set to $rfunction_id" [[ -n $dfunction_id ]] && echo "-d was set to $dfunction_id" [[ -n $1 ]] && echo "File is $1" 

Источник

Bash: Test If File Exists

While creating a bash script, it is commonly helpful to test if file exists before attempting to perform some action with it.

This is a job for the test command, that allows to check if file exists and what type is it.

As only the check is done – the test command sets the exit code to 0 ( TRUE ) or 1 ( FALSE ), whenever the test succeeded or not.

Also the test command has a logical “not” operator which allows to get the TRUE answer when it needs to test if file does not exist.

Cool Tip: Create a clever bash script! Make it do more tests! Check easily whether a string exists in a file! Read more →

Bash Shell: Test If File Exists

Run one of the below commands to check whether a file exists:

Test if the file /etc/passwd exist ( TRUE ):

$ test -f /etc/passwd $ echo $? 0 $ [ -f /etc/passwd ] $ echo $? 0

Test if the file /etc/bebebe exist ( FALSE ):

$ test -f /etc/bebebe $ echo $? 1 $ [ -f /etc/bebebe ] $ echo $? 1

Test if the file /etc/bebebe does not exist ( TRUE ):

$ test ! -f /etc/bebebe $ echo $? 0 $ [ ! -f /etc/bebebe ] $ echo $? 0

If File Exists, Then …

We usually test if a file exists to perform some action depending on the result of the test.

Maybe if file doesn’t exist – there is no sens to move forward and it is required to break the script or whatever.

Читайте также:  Создать базу данных mysql linux

In the examples below, lets print corresponding messages depending on the results of the test command.

Cool Tip: The CASE statement is the simplest form of the IF-THEN-ELSE statement! If you have many ELIF elements – it is better to use the CASE ! Read more →

Test if the file /etc/passwd exists and print a message if it is TRUE :

$ if [ -f "/etc/passwd" ]; then echo "The file exists"; fi The file exists

Test if the file /etc/bebebe does not exist and print a message if it is TRUE :

$ if [ ! -f "/etc/bebebe" ]; then echo "The file does not exist"; fi The file does not exist

Test if the file exists and print a corresponding message in the each case:

$ [ -f "/etc/passwd" ] && echo "The file exists" || echo "The file does not exist" The file exists $ [ -f "/etc/bebebe" ] && echo "The file exists" || echo "The file does not exist" The file does not exist

Bash Script: Test If File Exists

Lets create a bash script, that will check whether a passed as an argument file exists or not, by printing a corresponding message.

Create an empty checkfile.sh file with the touch checkfile.sh command.

Make it executable with chmod +x checkfile.sh .

Open the checkfile.sh with a text editor and put the following code:

#!/bin/bash FILE=$1 if [ -f $FILE ]; then echo "The file '$FILE' exists." else echo "The file '$FILE' in not found." fi

Cool Tip: A good bash script prints usage and exits if arguments are not provided! It is very simple to configure! Read more →

Save and execute the script as follows:

$ ./checkfile.sh /etc/bebebe The file '/etc/bebebe' is not found. $ ./script.sh /etc/passwd The file '/etc/passwd' exists.

Test If File Exists And It Is …

In the examples above with the -f operator we only check whether a file exists and it is a regular file.

Here are some other useful options that can help to check whether a “file” exists and has specific permissions or it is a symbolic link, socket or a directory:

Option Description
-e Test if file exists, regardless of type (directory, socket, etc.)
-f Test if file exists and is a regular file
-r Test if file exists and read permission is granted
-w Test if file exists and write permission is granted
-x Test if file exists and execute permission is granted
-L Test if file exists and is a symbolic link
-S Test if file exists and is a socket
-d Test if directory exists

Run man test to see all available operators.

Источник

Оцените статью
Adblock
detector