Sponsored Content
Top Forums Shell Programming and Scripting check file exist before execution Post 302629975 by zaxxon on Wednesday 25th of April 2012 08:39:16 AM
Old 04-25-2012
Check out the -e switch of the test command. You can also take single square brackets to resemble the test command or use double square brackets to use the shell's internal test.
You can also have a look into the man page of test or your shell to see which switches are available.

And please use code tags, even this up there is just pseudo code, thanks.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

how to check if the file exist or not?

say i would like to check if the file is existed before i use rm command. How can i do it? i know if i can use find, but i would like to have a good interface (in a shell script) thks (3 Replies)
Discussion started by: gusla
3 Replies

2. Shell Programming and Scripting

how to check if directory/file exist using c/c++

Hi there,, how to check if directory/file exist using c/c++ under unix/linux? I can use access() under Window MFC. Thanks. Steven (1 Reply)
Discussion started by: steven88
1 Replies

3. Programming

how to check if directory/file exist using c/c++

Hi there, how to check if directory/file exist using c/c++ under linux/unix. Thanks. Steven (2 Replies)
Discussion started by: steven88
2 Replies

4. Shell Programming and Scripting

Check Word if exist on file or not

Hello, I want to check if some word exist or not on some file By Example : word is : nixcraft file called : /root/shell.txt and i want to check if nixcraft word exist on /root/shell.txt file with if statement or another tool Any Ideas (5 Replies)
Discussion started by: LinuxCommandos
5 Replies

5. 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

6. Shell Programming and Scripting

Check file exist issue

I have created two scripts, one with hardcoded and another one with extract from file instead of hardcoded, script:1 -------- #!/bin/ksh filename="$one/file1.dat" if then echo "$filename has arrived." >> $logfile else echo "$filename has NOT yet arrived." >> $logfile fi :> Result:... (4 Replies)
Discussion started by: gksenthilkumar
4 Replies

7. 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

8. 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

9. Shell Programming and Scripting

how to check file exist in a directory or not

HI folks, can any one tell me how to check whether the file is existed in a directory or not . let me tell you my requirement : if the file is existed i should display a one message or else i have to send a mail .. i have the mail logic .. but I'm failed to check file existence .. please... (5 Replies)
Discussion started by: sravan008
5 Replies

10. Shell Programming and Scripting

Check for a file and touch if not exist

Hi, I've a situation where i need to check for the file existence and create a zero byte file based on the parameter,in some cases i need to touch and in some case i dont need to touch with zero byte file please help me on parameterizing this touch command?? Regards. San (2 Replies)
Discussion started by: sandeep karna
2 Replies
test(1) 						      General Commands Manual							   test(1)

NAME
test, [ - Evaluates conditional expressions SYNOPSIS
test [expression] [[expression]] STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: test: XCU5.0 [: XCU5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. OPTIONS
None OPERANDS
The expression to be evaluated as true or false. This expression is constructed from the operators and elements explained in the DESCRIP- TION section. DESCRIPTION
The test command evaluates an expression constructed of functions and operators. If the value of expression is true, test returns an exit value of zero(0); otherwise, it returns FALSE, a nonzero exit value. The test command also returns a nonzero exit value if there are no arguments. The alternate form of the command surrounds expression with brackets ([ ]). When you use this form, you must surround the brackets with spaces. The test Expressions All of the listed functions and operators are separate arguments to test. The following functions are used to construct expression: TRUE if file exists and has read permission. TRUE if file exists and has write permission. TRUE if file exists and has execute permission. TRUE if file exists and is a regular file. TRUE if file exists and is a directory. TRUE if file exists. TRUE if file exists and is a character-special file. TRUE if file exists and is a block-special file. TRUE if file exists and is a named pipe (FIFO). [Tru64 UNIX] TRUE if file exists and is a soft link. Synonym for -L expression. TRUE if file exists and is a soft link. Synonym for -h expression. TRUE if file exists and its set-user ID bit is set. TRUE if file exists and its set-group ID bit is set. [Tru64 UNIX] TRUE if file exists and its sticky bit is set. TRUE if file exists and has a size greater than zero(0). TRUE if the open file with file descriptor number file_descriptor (1 by default) is associated with a terminal device. TRUE if the length of string1 is zero(0). TRUE if the length of string1 is nonzero. TRUE if string1 and string2 are identical. TRUE if string1 and string2 are not identical. TRUE if string1 is not the null string. TRUE if the integers number1 and number2 are algebraically equal. Any of the comparisons -ne, -gt, -ge, -lt, and -le can be used in place of -eq. The listed functions can be combined with the following operators: Unary negation operator. Binary AND operator. Binary OR operator (the -a operator has higher precedence than the -o operator). [Tru64 UNIX] Parentheses for grouping. EXIT STATUS
The following exit values are returned: The test command evaluated expression and its value is TRUE. The test command evaluated expression and is value is FALSE, or there are no arguments. An error occurred. EXAMPLES
To test whether a file exists and is not empty, enter: if test ! -s "$1" then echo $1 does not exist or is empty. fi If the file specified by the first positional parameter to the shell procedure does not exist, this displays an error message. If $1 exists, it displays nothing. There must be a space between -s expression and the file name. The double quotes around $1 ensure that the test will work properly even if the value of $1 is the empty string. If the double quotes are omitted and $1 is the empty string, test displays the error message test: parameter expected. To do a complex compari- son, enter: if [ $# -lt 2 -o ! -s "$1" ] then exit fi If the shell procedure was given fewer than two positional parameters or the file specified by $1 does not exist, then this exits the shell procedure. The special shell variable $# represents the number of positional parameters entered on the command line that started this shell procedure. ENVIRONMENT VARIABLES
The following environment variables affect the execution of test: Provides a default value for the internationalization variables that are unset or null. If LANG is unset or null, the corresponding value from the default locale is used. If any of the internationalization vari- ables contain an invalid setting, the utility behaves as if none of the variables had been defined. If set to a non-empty string value, overrides the values of all the other internationalization variables. Determines the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multibyte characters in arguments). Determines the locale for the for- mat and contents of diagnostic messages written to standard error. Determines the location of message catalogues for the processing of LC_MESSAGES. SEE ALSO
Commands: csh(1), find(1), ksh(1), Bourne shell sh(1b), POSIX shell sh(1p) Standards: standards(5) test(1)
All times are GMT -4. The time now is 11:07 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy