Conditional execution of statements

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Conditional execution of statements
# 1  
Old 09-28-2011
Conditional execution of statements

Hi ,

I have a script which has multiple awk and sed commands like below.

First one :- 
find /root/src/  -name "*csv" -size 0 -exec rm {} \;

Second one: -
ls *SRE*.txt > SRT_TRAN.dat
rm *asr*.txt
Third one :-
find /root/src/ -name '*.csv' | while read FILENAME ;
awk -F, -v fn="${FILENAME##*/}" '{print fn, $0}' OFS="," "$FILENAME" > "${FILENAME}.txt"

What I am trying to do is execute the second command if first command is succesfull , execute the thrid command if second command is sucesfull, some times I will not have any files with *SER* and *asr*, in these cases i don't want the script to fail., it should come out sucessfully with out any error code and the subsequent script should not execute.

Please tell me how to do this.

# 2  
Old 09-28-2011
In general you can do

command || exit 1

so whenever command returns a nonzero exit status, your program exits.

Or just once, at the top of your shell, do

set -e

and your program will exit whenever any program has a nonzero exit status.

This depends on the program's exit status, though, if the program wasn't aware of any error that won't do it. So it really depends on what you mean by 'fail'. Does 'fail' for your first command mean 'find zero files'? And so forth. Tell me what you're trying to do, not the way you want to do it...
# 3  
Old 09-28-2011
I don't understand your need, please clarify.
But maybe this can help :
If you want to ignore the error message that are displayed, you can just add the redirection of the error output to the null file. example:
rm *asr*.txt 2>/dev/null

Regarding the dependencies, you can use "if/then/elif/else/fi" or "case $VAR in .../condition) ...;;/esac" block to manage the dependencies, also see || and && operator

Last edited by ctsgnb; 09-28-2011 at 02:15 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python Conditional Statements Based on Success of Last Command

In shell scripting, I can create a conditional statement based on the success or failure (exit status)of a command such as: pinger() { ping -c 2 $remote_host >/dev/null 2>&1 ping_stat=$? } pinger if ]; then echo "blahblahblah" exit 0 fi how is this done using Python using... (3 Replies)
Discussion started by: metallica1973
3 Replies

2. Shell Programming and Scripting

Conditional execution

Here's an interesting (to me, anyway) little puzzle. Background: I have a process that restores a number of large(ish) files from tape backup. As an individual file is being written, it is owned by root, then the ownership is changed when that file is complete. Since this process can take... (3 Replies)
Discussion started by: edstevens
3 Replies

3. Shell Programming and Scripting

Conditional Execution of a Script.

I have a unix shell script more echo "Calling" ./ echo "Calling" ./ more sqlplus -s user1/password1@DB_SID @/tmp/storedprocedures/Hello.rcp I run However, I do not want to be... (3 Replies)
Discussion started by: mohtashims
3 Replies

4. Shell Programming and Scripting

search and conditional execution

Hi, I have a list of files with different filenames like nam0001.txt,pan0001.txt etc coming in /data/inbox from various places. I needed a shell script which would:- 1)searches for a word from the filenames (coming in inbox directory) provided in input parameter. 2)if found, put in... (2 Replies)
Discussion started by: Saiesh
2 Replies

5. Shell Programming and Scripting

Execution Problems with if statements

Hi all, I habe a file called test.log, which contain following data : 0.0 0.1 0.1 0.1 0.1 0.2 0.3 0.3 0.4 0.4 0.6 8.7 8.8 17.2 I want to show the data which gater than 9.0 But my script not working. (4 Replies)
Discussion started by: mnmonu
4 Replies

6. Shell Programming and Scripting

How to use awk or nawk by using Conditional Statements

Hi Guys! Anybody know how can I use a nawk or awk on a script and printing the NAME, SECTION (must be 410 or 411 or 414) and TOTAL COST of CLASS 1 and 3 combined must be greater than 50. See below desired output file. input.txt: NAME,CLASS,COST,SECTION JOHN,1,10,410 JOHN,2,20,410... (2 Replies)
Discussion started by: pinpe
2 Replies

7. Shell Programming and Scripting

Conditional execution

Hi All, I want to echo a message in case a system is reachable by ping or echo a different message in case it's not reachable. Sample code i wrote is ping localhost -n 2 | grep 'ttl' > ping_op; ls ping_op > /dev/null && drReachable=Alive; echo -e `date`: \\t "DR server is reachable" >>... (5 Replies)
Discussion started by: Mr. Zer0
5 Replies

8. Shell Programming and Scripting

unix script for conditional execution

Below is my shell script. I am trying to execute two different BTEQ scripts depending on the day of the week. So on a saturday I will execute a certain BTEQ script and on other weekdays I will run the other script. #!/bin/ksh dt=`date +"%a"` if then bteq > final_output <<- EOF .run... (3 Replies)
Discussion started by: Mihirjani
3 Replies

9. Shell Programming and Scripting

for i loop with conditional statements?

New to scripting in general, so patience plz. If I ask a stupid question or don't get it, I thank you for your kindness in advance. That said, did a for i loops checks to see if a PB* file is there but I need to know two things before I copy the file. I need to know if the file's create date... (2 Replies)
Discussion started by: xgringo
2 Replies

10. Shell Programming and Scripting

Conditional Statements

How can I compare two decimal values within a function using Bash? Function fun2 isn't comparing the decimal values. Is there a way to do this using Bash or Korn? #!/bin/bash set -x x=1 z=110 function fun1() { i=`bc << EOF 2>> /dev/null scale=3 ... (1 Reply)
Discussion started by: cstovall
1 Replies
Login or Register to Ask a Question