Execute shell script and if string found while executing then exit


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Execute shell script and if string found while executing then exit
# 1  
Old 09-09-2014
Execute shell script and if string found while executing then exit

Hi All,

I have one shell script start.sh which executes another shell script test.sh something like below :
Code:
test.sh -param1 -param2

In the test.sh there is one command for removing file:
Code:
rm file1.bak

I want whenever I execute start.sh, it will execute test.sh and if it finds string rm file1.bak in test.sh, it does not execute it, exit and return back to start.sh script and then I will handle the rm statement from start.sh

Please suggest some solution. Thanks.

Last edited by rbatte1; 09-10-2014 at 09:56 AM.. Reason: Added CODE tags and emboldened commands
# 2  
Old 09-10-2014
That sounds over-engineered... why don't you perform that string search first, and then decide if the test script needs to be run at all? Following is my proposal, assuming both scripts are in the same directory:

start.sh
Code:
#!/bin/sh

#checking test.sh for occurrence of "rm file1.bak"
string="rm file1.bak"
if grep "$string" test.sh >/dev/null; then
 echo "String \"$string\" found in test.sh"
 echo "Executing rm command here and now ..."
 $string
else
 echo "Starting test.sh script ..."
 ./test.sh
fi

# 3  
Old 09-10-2014
But there are other statements also in test.sh which should be executed so cannot apply below method , the only issue is whenever I find rm statement in test.sh while executing it it should be executed from start.sh and not from test.sh.

Last edited by rbatte1; 09-10-2014 at 01:31 PM.. Reason: Added BOLD and correct capital letters
# 4  
Old 09-10-2014
How do you expect us to help without seeing your code?
From you you say in post#1 I suggest you replace in test.sh your line with rm by something else e.g. a flag you can use after in start.sh.
We can say more not knowing the code...
# 5  
Old 09-10-2014
Code:
./test.sh -param1 -param2

note that the 2nd script is called using a dot (./)
this dot notation runs the second script in the same shell ..i.e calling shell ..
if u call it without using a dot ...then also the script will run ... but in a new shell ..( sub-shell)
# 6  
Old 09-10-2014
@Makarand Dodmis please do not confuse:
that dot is because most certainly that script is not in $PATH and so is the only way to be executed when you get the shell complaining and outputs test.sh not found other than giving the full path to it (when it is found in current directory...)
not the same as
. /test.sh - Notice the space after the dot? This is "sourcing"
./test.sh .. - This is "execute - to be found in current directory"

Last edited by vbe; 09-10-2014 at 04:45 PM.. Reason: to Makarand Dodmis
# 7  
Old 09-10-2014
I am not using "." anywhere ...i am executing test.sh from start.sh and it is running fine . no issues with that

-- start.sh script

$path/$1 -param1 -param2


so i run ./start.sh $filename

this filename is passed inside start.sh as $1, right now $1 is test.sh . I have to pass 100 similar files like test.sh and all these files are using command -> rm $somefile.I have to perform more function along with rm so i dont want to change all the 100 files , i want to handle it in start.sh . so i want when executing start.sh , it executes test.sh and when it goes to rm command in test.sh it ignores it /skips it whatever , exit from test.sh and control is return back to start.sh .. i will handle rm and other extra function from start.sh after that.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to keep staying on remote server after executing a shell script with if then exit end statement?

i have a "if .. then exit end " in s shell script on remote servers. now the connection to the remote server got killed after i run this script on the remote servers. How do i run this script on remote hosts and still keep remote connections alive after executing the script. Thank you. (10 Replies)
Discussion started by: moonmonk
10 Replies

2. Shell Programming and Scripting

Executing 'exit' command from shell script

Hi, I am writing shell script to automate few use cases for CLI interface. We have CLI interface which has bunch of commands. I am trying to execute one of the commands 'exit' as part of automation to exit from CLI object (not from shell script) in my shell script. My intension is to execute... (4 Replies)
Discussion started by: Mahesh Desai
4 Replies

3. Shell Programming and Scripting

Exit while loop on execute script

Hi, I have first script which on IR remote command event execute the second script. If the second script is executed, it display echo "timeout expired" after 10s. This works as expected. But I also want to reset timer (increase time) in case if the second script is executed again within 10s. ... (8 Replies)
Discussion started by: armatron
8 Replies

4. UNIX for Dummies Questions & Answers

Launch shell script if string match is found

I'm trying to write a simple shell script that looks at a given text file and if the only word in the file is 'completed', it launches another shell script. So far I have this almost working... if grep 'completed' $datafile then... however, using this logic the secondary shell script... (3 Replies)
Discussion started by: MickeyGreen
3 Replies

5. Shell Programming and Scripting

When i am trying to execute export command within a shell script it is saying command not found.

I am running the export command within a view to use that value inside my build script. But while executing it it is saying "export command not found" My code is as follows: -------------------------- #!/bin/sh user="test" DIR="/bldtmp/"$user VIEW="test.view1" echo "TMPDIR before export... (4 Replies)
Discussion started by: dchoudhury
4 Replies

6. Shell Programming and Scripting

Execute script if a file is found

I'm trying to get the following to look for the newest powerpoint presentation and if it finds something then run a shell script named star_presentation.sh and if it doesn't then simply exit. Any help would be greatly appreciated. #!/bin/bash ppts=/ticker/powerpointshare find $ppts... (3 Replies)
Discussion started by: binary-ninja
3 Replies

7. Shell Programming and Scripting

getting the error 'not found' while executing the script

Hi, I am not able to figure out what the problem is: getting the following error sqltst.sh: 1: not found here is the script #!/bin/sh . /home/dev1/.profile . /home/dev1/.infenv `sqlplus -s $REPDB_LOGON << EOF SET SERVEROUT ON SET FEEDBACK OFF SET HEADING OFF SET TRIMSPOOL... (4 Replies)
Discussion started by: svajhala
4 Replies

8. UNIX for Dummies Questions & Answers

nohup - sub job in script not executing until I exit

My job is launched using this command: I'm at home and having VPN drops so I used nohup and background. nohup perf_mon -c rating_4_multi,cfg & The main script is PID 26119, and the sub task under it is 26118 which is not running - just sits there. 26119 runs forever but nothing else runs. I... (2 Replies)
Discussion started by: ido1957
2 Replies

9. UNIX for Dummies Questions & Answers

Checking the header and trailer for a given string and if not found, exit out of the

hi, How to check a given file for a string and if it's not found, exit out ofthe script? e.g. a file Test123 is there whose header begins with #bt and trailer begins with #ed. I have to check if the header and trailer matches as above and if not, exit out of the script. How can we do it in... (2 Replies)
Discussion started by: er_ashu
2 Replies
Login or Register to Ask a Question