If statement help


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If statement help
# 1  
If statement help

I'm trying to create a script that would allow me to identify the sucessful removal of a file. Here's what i put together so far, let me know if it's correct or not.

FILE_NAME="cactus.dat"
FILE_FIND='find / -name $FILE_NAME'

if [ -z $FILE_NAME ] ;then
echo "cactus.dat was not removed successfully"
else
echo "cactus.dat was removed successfully"
fi
# 2  
The -z test will not work in Bourne shell. It will in Korn - not sure about Bash.
The FILE_FIND command needs backticks - not quotes.
Also, in some forms of 'find' you need a -print command.

So...
Code:
FILE_NAME="cactus.dat"
FILE_FIND=`find / -name $FILE_NAME -print`

if [ -z $FILE_FIND ] ;then
# Zero string length - file not found
echo "cactus.dat was removed successfully"
else
echo "cactus.dat was not removed successfully" 
fi

The -z test will not work in Bourne shell. It will in Korn - not sure about Bash. Also, in some forms of 'find' you need a -print command.

Jerry
# 3  
Quote:
Originally Posted by JerryHone
The -z test will not work in Bourne shell. It will in Korn - not sure about Bash.

The -z test will work in all Bourne-type shells.
Quote:
Also, in some forms of 'find' you need a -print command.

If you have an old version of find that requires -print (unlikely), you can test success directly:

Code:
if find / -name "$FILE_NAME"; then

# 4  
Quote:
Originally Posted by sdpinoy
I'm trying to create a script that would allow me to identify the sucessful removal of a file. Here's what i put together so far, let me know if it's correct or not.

Code:
FILE_NAME="cactus.dat"
FILE_FIND='find / -name $FILE_NAME'


Use backticks or $( ... ) to capture the output of a command, and quote your variables:

Code:
FILE_FIND=$( find / -name "$FILE_NAME" )

Quote:
Code:
 
if [ -z $FILE_NAME ] ;then


Don't you mean:

Code:
if [ -z "$FILE_FIND" ] ;then

Quote:
Code:
 echo "cactus.dat was not removed successfully" 
else
 echo "cactus.dat was removed successfully"
fi

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #60
Difficulty: Easy
BCD stands for 'Binary Coded Decimal'.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

If statement help

I am new to Unix and have been given the task of modifying a current script. Most of the programming I do is Oracle based, not unix. That being said when I run my current script i get the following (not sure if this is an actual error or if it is just being informative) ./test_cron.s: cdev:... (3 Replies)
Discussion started by: biobill
3 Replies

3. Shell Programming and Scripting

What does this statement do?

Hi, What does this line do in bash? MAILDROP_GROUP="@@"; Is this just assigning a string? or does it have special meaning? Thanks in advance. Wilsonee (3 Replies)
Discussion started by: wilsonee
3 Replies

4. Shell Programming and Scripting

Help with for statement within while statement

I'm trying to do a for statement within a while read line statement and it's not working. What is wrong with this? while read line do find . -type f -name "*.$line*" -exec gunzip {} \; for file in */*.$line*; do grep ERROR "*.$file*" > error.$line.log; done find . -type f -name... (1 Reply)
Discussion started by: bbbngowc
1 Replies

5. Shell Programming and Scripting

if statement

I am having problem writing the following script, this is more like a boolean case where i need to check for existance of a string in a file and if it exist i have to do certain things. If ( such string exist in a file) sed 's/car/bus/g' file else do something else. (7 Replies)
Discussion started by: ROOZ
7 Replies

6. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

7. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies

8. UNIX for Advanced & Expert Users

IF statement on a df -g

Im running reports on a unix box. i run df -g and get the disk usage results, what i want to do is hopefully run an if statement or of the sort to highlight any percentages over 80 %. I also would like to run an errpt and also highlight any errors for the current days date. Could the results... (1 Reply)
Discussion started by: hassanj
1 Replies

9. Shell Programming and Scripting

If statement

Hi, I have a statement in shell script which i am writing below: if ] then ..... What is the meaning of -a option in files, meaning of the statement in the if condition ] means. Thanks (3 Replies)
Discussion started by: dreams5617
3 Replies

10. Shell Programming and Scripting

if statement

I have below program to find the process "orapp" and echo the result , if xxxx then for x in `ps -ef |grep "orapp" |awk { print $2; } do echo $x done else xxxx fi now if I want to add one more condition - if the IP address of the process is 192.168.0.1 ( who -u |awk { print $8} ,... (2 Replies)
Discussion started by: ust
2 Replies

Featured Tech Videos