IF statement file exist or not?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting IF statement file exist or not?
# 1  
Old 03-31-2012
IF statement file exist or not?

How can I check and see if the first parameter is a file existing in the current path or it is a real file and user input some random name in an IF statement?
# 2  
Old 03-31-2012
Code:
if [ -f $1 ] ; then
    echo "$1 is a file in the current working directory"
fi

# 3  
Old 03-31-2012
what if I mix it with getopts command and check for input options. first parameter ($1) would be always a wrong file because it is (eg. -u) one of the switches!
# 4  
Old 03-31-2012
First use getopts to get the options, then shift the parameters to its proper position, through
Code:
shift $(($OPTIND - 1))

If you use
Code:
if [ -f "$1" ] ; then

then the filename may also contain spaces and/or other special characters...
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-31-2012
I couldn't figure it out! would you take a look at this:
Code:
usage()
{
cat << EOF

USAGE: $0 [options] file

EOF
}

usageOption()
{
cat << EOF

$0 Must select report type: -u(ser) or -c(ommand)

EOF
}

clear

if [ $# -lt 1 ]; then
    usage
    exit 1
fi

user=
command=
fileName=
totalTime=
memoryUse=
priority=
#OPTIND=1         # Reset in case getopts has been used previously in the shell
while getopts ":u:c:f:tmp" opt;do
 case $opt in
    u)    user=$OPTARG; echo "$user" >&2;;
    c)    command=$OPTARG;;
    f)    fileName=$OPTARG;;
    t)    totalTime=$OPTARG;;
    m)    memoryUse=$OPTARG;;
    p)    priority=$OPTARG exit;;
    \?)    usage;exit 1;;
    *)        echo "Option -$OPTARG requires an argument.";exit 1;;
 esac
done
shift $((OPTIND - 1))

if [ -f "$1" ] ; then
    echo "This is a test!"
fi

# 6  
Old 03-31-2012
This:

Code:
if [ $# -lt 1 ]; then
    usage
    exit 1
fi

should be below the shift, because you are testing if there is a first parameter after the command line options...

t, m and p also need to be followed by a colon, since you are using their $OPTARG in the case statement.

You can leave out *) because it will never get there. Because of the leading : in the opt string $opt will be set to "?" for unknown flags..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File not exist in direct

friends can do this from unix AIX I need to ask for a file that does not exist then if file.txt * **** echo "execute procedure" else *** echo "File does not exist" if You can sucedere that the file does not exist (2 Replies)
Discussion started by: tricampeon81
2 Replies

2. Shell Programming and Scripting

file exist in folder

Hi guys, I am new to shell script, i want to write shell script, the script details has mentioned below Two files are to be check in one particular directory, if the file exist in particular folder then the file is existed and also if the file exist more than three hours then send... (2 Replies)
Discussion started by: madhavi_bodi
2 Replies

3. Shell Programming and Scripting

Grepping file and returning passed variable if the value does not exist in file at all.

I have a list of fields that I want to check a file for, returning that field if it not found at all in the file. Is there a way to do a grep -lc and return the passed variable too rather then just the count? I am doing some crappy work-around now but I was not sure how to regrep this for :0 so... (3 Replies)
Discussion started by: personalt
3 Replies

4. Shell Programming and Scripting

Check if file exist

Hi, I created following script to check if file exist: #!/bin/bash SrcDir=$1 SrcFileName=$2 SrcTimePeriod=$3 if ;then echo 1 else echo 0 fi I ran it like: /apps/Scripts/FileExist.sh /apps/Inbox file1 2nd_period_2010 Even file exist at that location, my above command is... (4 Replies)
Discussion started by: palak08
4 Replies

5. Shell Programming and Scripting

Check if file exist

Hi, I am trying to create a bash script which will check if file exist then remove that file else do nothing. I have to do same process for three files in same script. I have written code for one file and trying to run it. if then rm -r /user1/abc/File1 fi When I run this code it... (1 Reply)
Discussion started by: palak08
1 Replies

6. Shell Programming and Scripting

How to Check whether list file present in TXT file exist or not

Hi All, I have txt file which has list of files. I have to check whether these files exist or not. Thanks supriya (6 Replies)
Discussion started by: supriyabv
6 Replies

7. Shell Programming and Scripting

Check if file exist

Hi Does anybody know how I can check if a file exists i.e. see bellow, this doesn't work by the way and if tried countless variations on this file1=$one/file111.txt if then echo "Present" else echo "Not present" fi result : Not present (file is already present, eventhough its... (3 Replies)
Discussion started by: gksenthilkumar
3 Replies

8. Shell Programming and Scripting

File exist test

Can someone please shed light on why this may not be working, file does exist, but I get an error if ] then echo "No ${source_path}/${file_mask} found - ">> ${logfile} result=1 check_result ${result} "Failed to find file... (4 Replies)
Discussion started by: Pokermad
4 Replies

9. Shell Programming and Scripting

If file not exist create a new one

i want a script to check whether the file name exits or not if not it has to create a new one (3 Replies)
Discussion started by: din_annauniv
3 Replies

10. Shell Programming and Scripting

Have a shell script check for a file to exist before processing another file

I have a shell script that runs all the time looking for a certain type of file and then it processes the file through a series of other scripts. The script is watching a directory that has files uploaded to it via SFTP. It already checks the size of the file to make sure that it is not still... (3 Replies)
Discussion started by: heprox
3 Replies
Login or Register to Ask a Question