usage of case statement in place of IF elif...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting usage of case statement in place of IF elif...
# 1  
Old 10-08-2009
usage of case statement in place of IF elif...

Hi,

I need to carry out the back up of the data if exists...(file size not equal to zero)

i tried in this way but it is not successful....where am making the mistakes? and if possible can i use case syntax in place of "if"

Code:
#!/bin/ksh
filename=`TZ=CST+24 date +%Y%m%d`
ZERO=0
SIZE_64=$(ls -ltr /aemu/ErrorLogs/data/MissingCGI-64.txt | tr -s ' ' | cut -d ' ' -f 5)
SIZE_71=$(ls -ltr /aemu/ErrorLogs/data/MissingCGI-71.txt | tr -s ' ' | cut -d ' ' -f 5)
SIZE_72=$(ls -ltr /aemu/ErrorLogs/data/MissingCGI-72.txt | tr -s ' ' | cut -d ' ' -f 5)
SIZE_73=$(ls -ltr /aemu/ErrorLogs/data/MissingCGI-73.txt | tr -s ' ' | cut -d ' ' -f 5)
SIZE_80=$(ls -ltr /aemu/ErrorLogs/data/MissingCGI-80.txt | tr -s ' ' | cut -d ' ' -f 5)
SIZE_EZ=$(ls -ltr /aemu/ErrorLogs/data/MissingCGI-EZ.txt | tr -s ' ' | cut -d ' ' -f 5)
SIZE_WZ=$(ls -ltr /aemu/ErrorLogs/data/MissingCGI-WZ.txt | tr -s ' ' | cut -d ' ' -f 5)
SIZE_NZ=$(ls -ltr /aemu/ErrorLogs/data/MissingCGI-NZ.txt | tr -s ' ' | cut -d ' ' -f 5)
SIZE_MTNL=$(ls -ltr /aemu/ErrorLogs/data/MissingCGI-MTNL.txt | tr -s ' ' | cut -d ' ' -f 5)
SIZE_COUNT=$(ls -ltr /aemu/ErrorLogs/data/MissingCGIcount.txt | tr -s ' ' | cut -d ' ' -f 5)



if [ $SIZE_64!=$ZERO ]
        then
        cp /aemu/ErrorLogs/data/MissingCGI-64.txt /aemu/ErrorLogs/backup/CHN/$filename
sleep 2
        sed '' /aemu/ErrorLogs/data/MissingCGI-64.txt > /aemu/ErrorLogs/data/MissingCGI-64.txt

elif [ $SIZE_71!=$ZERO ]
        then
        cp /aemu/ErrorLogs/data/MissingCGI-71.txt /aemu/ErrorLogs/backup/KTK/$filename
sleep 2
        sed '' /aemu/ErrorLogs/data/MissingCGI-71.txt > /aemu/ErrorLogs/data/MissingCGI-71.txt
        
elif [ $SIZE_72!=$ZERO ]
        then
        cp /aemu/ErrorLogs/data/MissingCGI-72.txt /aemu/ErrorLogs/backup/KER/$filename
sleep 2
        sed '' /aemu/ErrorLogs/data/MissingCGI-72.txt > /aemu/ErrorLogs/data/MissingCGI-72.txt

elif [ $SIZE_73!=$ZERO ]
        then
        cp /aemu/ErrorLogs/data/MissingCGI-73.txt /aemu/ErrorLogs/backup/AP/$filename
sleep 2
        sed '' /aemu/ErrorLogs/data/MissingCGI-73.txt > /aemu/ErrorLogs/data/MissingCGI-73.txt

elif [ $SIZE_80!=$ZERO ]
        then
        cp /aemu/ErrorLogs/data/MissingCGI-80.txt /aemu/ErrorLogs/backup/TN/$filename
sleep 2
        sed '' /aemu/ErrorLogs/data/MissingCGI-80.txt > /aemu/ErrorLogs/data/MissingCGI-80.txt
        
elif [ $SIZE_EZ!=$ZERO ]
        then
        cp /aemu/ErrorLogs/data/MissingCGI-EZ.txt /aemu/ErrorLogs/backup/EZ/$filename
sleep 2
        sed '' /aemu/ErrorLogs/data/MissingCGI-EZ.txt > /aemu/ErrorLogs/data/MissingCGI-EZ.txt

elif [ $SIZE_WZ!=$ZERO ]
        then
        cp /aemu/ErrorLogs/data/MissingCGI-WZ.txt /aemu/ErrorLogs/backup/WZ/$filename
sleep 2
        sed '' /aemu/ErrorLogs/data/MissingCGI-WZ.txt > /aemu/ErrorLogs/data/MissingCGI-WZ.txt
        
elif [ $SIZE_NZ!=$ZERO ]
        then
        cp /aemu/ErrorLogs/data/MissingCGI-NZ.txt /aemu/ErrorLogs/backup/NZ/$filename
sleep 2
        sed '' /aemu/ErrorLogs/data/MissingCGI-NZ.txt > /aemu/ErrorLogs/data/MissingCGI-NZ.txt
        
elif [ $SIZE_MTNL!=$ZERO ]
        then
        cp /aemu/ErrorLogs/data/MissingCGI-MTNL.txt /aemu/ErrorLogs/backup/MTNL/$filename
sleep 2
        sed '' /aemu/ErrorLogs/data/MissingCGI-MTNL.txt > /aemu/ErrorLogs/data/MissingCGI-MTNL.txt
        
else [ $SIZE_COUNT!=$ZERO ]
        cp /aemu/ErrorLogs/data/MissingCGIcount.txt /aemu/ErrorLogs/backup/COUNT/$filename
sleep 2
        sed '' /aemu/ErrorLogs/data/MissingCGIcount.txt > /aemu/ErrorLogs/data/MissingCGIcount.txt
        
fi

exit

Aemunathan
# 2  
Old 10-08-2009
From what I can tell, you should have spaces around " != " as in
Code:
[ $SIZE_64 != $ZERO ]

# 3  
Old 10-08-2009
Hi

If any one condition satisfied in the if statement, it come out of all the conditional loops...
in this case it come out after executing first if statement...eventhough other conditions are also true.
What could be the issue?
# 4  
Old 10-09-2009
I think that is the functionality of else-if ladder. It will check the first if condition, if its false goes to next if (elif) condition, if its false next elif condition or if the condition returns true it comes out of whole if.

If all the conditions are getting false then it will execute the else part.

If you want your script to execute all the if conditions use

if ..
then
.
.
.
fi

for each checking.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert to case statements from if/elif

Hello, I wrote the case on code but it mistakes. I am not sure. If/elif code: #!/bin/ksh you=$LOGNAME hour=`date | awk '{print substr($4, 1, 2)}'` print "The time is: $(date)" if (( hour > 0 && $hour < 12 )) then print "Good morning, $you!" elif (( hour == 12 )) then (7 Replies)
Discussion started by: Masterpoker
7 Replies

2. Homework & Coursework Questions

Case Statement

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hey, guys I really need some help with a project. "Write a shell program that examines the command line... (8 Replies)
Discussion started by: sk192010`
8 Replies

3. Shell Programming and Scripting

Case Statement

Hey, guys I really need some help with a project. "Write a shell program that examines the command line arguments, counts and collects the number of options. Basically it has to collect and count the arguments that start with a "-" and the one's that don't start with a - I know I have to use... (2 Replies)
Discussion started by: sk192010`
2 Replies

4. Shell Programming and Scripting

case statement

Hi, I am writing case statement to execute some finction, my requirement is once one of the case statement is executed again it has to prompt for the option. for script in `echo "$Script_Selected"` do case $script in 1) getNoOFActUsers ;; 2) moveServerrOORotation ;; ... (2 Replies)
Discussion started by: Satyak
2 Replies

5. Shell Programming and Scripting

help with case statement

I am writing a script to pull diskspace information from our servers. Here is the script that I wrote: #!/bin/ksh for host in `cat /oper/hosts/esc.misc` do ssh -q -o ConnectTimeout=10 operator@$host df -h|grep "/dev/" |egrep '8%|9%|100%' | awk '{print H " " "at " $5 " with " $4 "... (1 Reply)
Discussion started by: rkruck
1 Replies

6. Shell Programming and Scripting

case statement

Hi all, I think i'm asking a sqtupid question here.. i'm using case sttament, what is the syntax or symbol for "or"? I thought was || here a quick sample of my case statment echo "Would you like to update your detail ?" read response case $response in ... (2 Replies)
Discussion started by: c00kie88
2 Replies

7. UNIX for Dummies Questions & Answers

If or Case Statement

I want to write a program with the following variables: a=7000 b=24000 c=613.8 The user can enter two words: Vivid or Blue for example. The challenge is that the user might not want to write the words the way they appear. The user can write V or v or vivid or Vivid or write Blue or blue, or B,... (1 Reply)
Discussion started by: Ernst
1 Replies

8. Shell Programming and Scripting

How can I place a for statement....

In a script? I have some folders that contain many files of 8MB or more in size. Every day run "for file in F*; do gzip $file; done" How can I put this in a script to run automatically? (7 Replies)
Discussion started by: bbbngowc
7 Replies

9. UNIX for Dummies Questions & Answers

Help with ELIF statement

I am receiving an elif error on line 13 and I can not figure out the reasoning behind it. I have added the then statement that I was initially missing. Any help would be great. #The purpose of this script is for the end user to be able to enter a positive number #User enters a number NUM=$1... (4 Replies)
Discussion started by: Brewer27
4 Replies

10. Shell Programming and Scripting

case statement

Hi all, is it possible to create a 'dynamic' case statement. ie select option in `ls` do case satement depending on results of the above `ls` done I hope I have explained this ok! Thanks Helen (1 Reply)
Discussion started by: Bab00shka
1 Replies
Login or Register to Ask a Question