Visit Our UNIX and Linux User Community


Script Execution Order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script Execution Order
# 1  
Old 02-24-2015
Hammer & Screwdriver Script Execution Order

Hi,

I have two scripts which are mentioned in execute.sh

Code:
more execute.sh
./script1.sh   //line 1  should not return error
./script2.sh   //line 2  may return error
./script2.sh   //line 3  should not return error

Condition: I want script1.sh to complete before starting script2.sh (line 2) and script2.sh (on line 2) to complete before starting the script2.sh (on line 3)

Also there should be no errors in execution of line 1 and line 3 scripts while i expect errors returned in execution of line 2. Thus, i need a check at the end of the three lines if line 1 or line 3 returned errors (should display what returned error) execute.sh failed or else execute.sh was successful.

Can you please help ?
# 2  
Old 02-24-2015
Quote:
Originally Posted by mohtashims
Condition: I want script1.sh to complete before starting script2.sh (line 2) and complete script2.sh (on line 2) before starting the script2.sh (on line 3)
This is normally the case. If your scripts contain no sort of exquisite process management features (sending parts of themselvs into background, using nohup or the like) they way you wrote it script1 should be executed, then, upon its completion, script2 should be started, etc..

What makes you think this is not the case?

Quote:
Originally Posted by mohtashims
Also there should be no errors in execution of line 1 and line 3 scripts while i expect errors returned in execution of line 2. Thus, i need a check at the end of the three lines if line 1 or line 3 returned errors (should display what returned error) execute.sh failed or else execute.sh was successful.
You can check the error code of a (any) process by querying the variable "$?", which is set after the completion of every command. Notice, though, that every other command re-sets this. For instance:

Code:
some_command
echo $?

will show the error returned by that command, but
Code:
echo "$(some_command | grep <something>)"
echo $?

will not. In fact it will display the error returned from the (first) echo command, which is most probably 0.

There are also other commands which use the error code returned from a process:

Code:
if some_command ; then
     echo "some_command returned error 0"
else
     echo "some_command returned an error >0"
fi

"if" evaluates the return level from the command (which can be a single command or some compound, like a pipeline) and executes the "then"-branch if this returns 0, otherwise the else-branch. Substitute "some_command" with "/bin/true" or "/bin/false" (two commands, which always return 0 and 1 respectively) to see the effect.

Another caveat is that scripts notoriously "forget" to set an error code. For a script to set an exit code you need to explicitly mention it with the exit-statement. This, for instance:

Code:
#! /bin/ksh

some_command
other command

exit

will always exit with 0, regardless of what the commands return. A script returning an error would look like this:

Code:
#! /bin/ksh

typeset -i iErrLvl=0

if ! some_command ; then
     iErrLvl=1
     print -u2 "Error in some_command"
else
     if ! other command ; then
          iErrLvl=2
          print -u2 "Error in some_command"
     fi
fi

exit $iErrLvl

This will execute "some_command" and return 1 if it fails, but if it succeeds it will also execute "other_command" and return 2 if this fails. If both succeed it will return 0.


I hope this helps.

bakunin

Last edited by bakunin; 02-24-2015 at 09:39 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #698
Difficulty: Medium
"Homesteading the Noosphere" (abbreviated HtN) is an essay written by Eric S. Raymond about the social workings of proprietary, corporate software development.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Order of execution of instructions in a script shell

Hi all, Here is a portion of my script test.sh: #!/bin/bash tar -cf $name.tar $name && pbzip2 $name.tar 2-Sending .tar.bz2 to destination node Please, i have a question. The execution of the second instruction is done, but i do not see the result of pbzip2 I have to send this... (2 Replies)
Discussion started by: chercheur111
2 Replies

2. Shell Programming and Scripting

Script to order my video library :-)

Dear friends, I have a hundred or so videos I keep in a directory, but as the collection has grown anarchically in the past years I have at least 3 different situations: a) one directory for each video and within it the video and any other accompaining files (mostly text files with my notes and... (5 Replies)
Discussion started by: rjalex
5 Replies

3. Solaris

Script on Solaris spawning 2 processes for one shell script execution

Hi, I am having a shell script on Solaris 10 which has a while loop as shown below. #!/usr/bin/ksh # while do sleep 60 done Name of the shell script is coldcentric.sh. I executed script /DATAWAREHOUSE/LOAD/Scripts/coldcentric.sh from a command task in Informatica worklow as... (3 Replies)
Discussion started by: chekusi
3 Replies

4. UNIX for Advanced & Expert Users

Order of execution seem to be reversed with pipe

Hello I am seeing a weird behavior during the execution of the shell program. The shell programs loops through a file which has a list of files. for each file the programs cats the file, pipes the output to sed command and then on to an awk program. But while execution, the order of... (3 Replies)
Discussion started by: vamxdir
3 Replies

5. Shell Programming and Scripting

Multiple Interval cron issue with order of execution

55,0 5,6 * * * myScript This cron task will execute on 5:00AM, 5:55AM, 6:00AM and 6:55AM. Is there any possibility to make it run only in the order specified such as 5:55AM and 6:00AM (basically only on the 2 intervals) ? (1 Reply)
Discussion started by: vikram3.r
1 Replies

6. Emergency UNIX and Linux Support

invoke one script based on previous script execution

I am database guy and not very good at shell scripts. I am seeking help to sharp my script coding. I have 5 scripts 1. master script. I use this one to call other four scripts to do database work. 2. db_backup_1 and log_backup_1 3. db_backup_2 and log_backup_2 in master script, I want to... (4 Replies)
Discussion started by: duke0001
4 Replies

7. UNIX for Advanced & Expert Users

SSH using shell script terminates the script execution

Hello, I am writing a shell script in which i do ssh to remote server and count the number of files there and then exit. After the exit the shell script terminates which i believe is expected behavior. Can some one suggest me a way where even after the exit the script execution resumes. ... (2 Replies)
Discussion started by: manaankit
2 Replies

8. UNIX for Advanced & Expert Users

How to set the SGE job execution order for nested qsub?

Dear All I am trying to write a script to set the SGE job execution order. I named each job with 'job1', 'job2' and 'job3'. I want my script to do: When 'job1' execution is complete, 'job2' is executed; when both 'job1' and 'job2' are complete, 'job3' is executed. First, I tried this... (0 Replies)
Discussion started by: cliffyiu
0 Replies

9. Solaris

Program execution order like FIFO queue

Hi all: I have a problem with a C++ service runing on solaris 10. This service only set a signal on oracle table. When the service detect a cut off on the line (tcp/ip), trigger a cobol program for set the signal OFF. When the line is on again, the service trigger the same cobol program for set... (0 Replies)
Discussion started by: hcastellani
0 Replies

10. UNIX for Dummies Questions & Answers

Changing the order of columns in a script

I have the following script: (echo -n `curl http://www.example.com/scores.txt | grep mylocation`; date +%Y%m%d%H%M%S) >> myscores.txt This script works fine, except that it places the timestamp at the end of the file myscores.txt. How do add the timestamp as the first column and then a tab and... (4 Replies)
Discussion started by: figaro
4 Replies

Featured Tech Videos