Sponsored Content
Top Forums Shell Programming and Scripting How to exit a script with error ? Post 302836439 by Salil Gupta on Wednesday 24th of July 2013 07:27:52 AM
Old 07-24-2013
How to exit a script with error ?

Hi,

I have a script ABC which calls another script XYZ. Function of XYZ is to ftp a file from one server to another.

code for ABC:
Code:
#!/bin/ksh
PATH=/usr/bin

home/user/xyz "$@"

exit $?
~


code for xyz:

Code:
#!/bin/ksh
HOSTNAME=$1
 SRCNAME=$2
  DSTNAME=$3

### This file holds the outget of the FTP commands for success checking
  FTPCHK=/tmp/ftpchk.$$
  #touch $FTPCHK
  echo "" > $FTPCHK

SRCFILE=$SRCNAME

DSTFILE=$DSTNAME


echo Source Host: $HOSTNAME
echo Source File Name: $SRCFILE
echo Destination File: $DSTFILE

echo `date` Beginning FTP download...
ftp -v ${HOSTNAME} <<EOF >>$FTPCHK 2>&1
 ascii
 nlist ${SRCFILE}
 get ${SRCFILE}  ${DSTFILE}
 quit
EOF

### Check for FTP errors
  CHK1=`grep -c "226 Transfer complete." $FTPCHK`
  CHK2=`grep -c "226 ASCII Transfer complete." $FTPCHK`
  CHK3=`grep -ic "226 transfer complete" $FTPCHK`
  CHK4=`grep -ic "226 File send OK." $FTPCHK`

  ### In NT, There should transmissions complete:
  ### One for the data transfer,
  ### One for the nlist

 if [ $CHK1 -eq 2 ] || [ $CHK2 -eq 2 ] || [ $CHK1 -eq 1 -a $CHK2 -eq 1 ] || [ $CHK3 -eq 2 ] || [ $CHK4 -eq 1 ]; then
    RC=0
    echo "OK: FTP Transmission Worked"
    echo '--------------------------------------------------------------'
    cat $FTPCHK
    echo '--------------------------------------------------------------'


  else
     RC=1
     echo "ERROR: FTP Transmission Failed"
     banner "ERROR"
     echo '--------------------------------------------------------------'
     cat $FTPCHK
     echo '--------------------------------------------------------------'
  fi

date
ls -al ${DSTFILE}

rm $FTPCHK

echo '---------------------------------------------------------------------'

exit $RC

In script XYZ, I am trying to ftp a file. So during ftp , whenever it shows that space is less than size of file, it should ftp the partial according to the available space and prompt a message about it and also prompt a message to outer script that inner script could not complete successfully. Hence resulting in the failure of outer script, due to partial ftp of file.

Also please post the comments about the each step of script to explain what is happening in that step. Thanks a lot in advance.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

exit on error - is it possible?

Hi , i have shell scripts that run the mysql directly by echoing and redirecting the output to the mysql logins. whenever the query executes successfully then the script runs fine and nothing has to be done there. Now, when there is an error executing the query then it will generate the error... (8 Replies)
Discussion started by: ahmedwaseem2000
8 Replies

2. Linux

tar error exit delayed form pervious error

Hi when use "tar cpvzf /dev/st0 --exclude=/proc --exclude-/lost+found --exclude=/mnt --exclude=/media --exclude=/sys /" to tape, get the following message "tar: error exit delayed form pervious error", What is the mean ? Please suggest some solution for these errors. thx (1 Reply)
Discussion started by: chayato
1 Replies

3. UNIX for Dummies Questions & Answers

trying to grep the first few lines of a continuos script, and exit the script anyidea

Hi. I am trying to extract the output of the first few lines of a continuos sh script. The when i run it, i wont to grep the the first 20 lines of it for an entry and basically do a crtl z out of it or to that effect, and output the results to a text file. I basically want to script... (5 Replies)
Discussion started by: k00061804
5 Replies

4. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

5. UNIX for Dummies Questions & Answers

aCC exit error

Hi guys I would just like to know if aCC supports the command exit(0); in c++? I am always getting the error below: Error 328: "ac5.C", line 37 # Function 'exit' has not been defined yet; cannot call. exit(0); ^^^^ Anyone had this problem? Thanks! (2 Replies)
Discussion started by: khestoi
2 Replies

6. Shell Programming and Scripting

If Error then Exit

HI I am just using cd Command and i want exit if error. Ex. cd /hp/pp/0720 If above folder in not available then stop the script Folder is change every day Thanks (3 Replies)
Discussion started by: asavaliya
3 Replies

7. Shell Programming and Scripting

How to get the exit status of a command in nner script to the outer script?

Hi all, I have a shell script inside which i am executing another shell script. In the inner script im executing a command. i want the status of that command in the outer script to perform some validations. How to get its status please help!!1 Im using ksh. (2 Replies)
Discussion started by: Jayaraman
2 Replies

8. Shell Programming and Scripting

How to capture exit code of child script and send it to parent script?

#!/usr/local/bin/bash set -vx /prod/HotelierLinks/palaceLink/bin/PalacefilesWait /prod/HotelierLinks/palaceLink/bin/prodEnvSetup 03212013 & if then echo "fatal error: Palace/HardRock failed!!!!" 1>&2 echo "Palace Failed" | mail -s "Link Failed at Palace/HardRock" -c... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

9. Shell Programming and Scripting

How to capture the exit code of a shell script in a perl script.?

hi, i want to pop up an alert box using perl script. my requirement is. i am using a html page which calls a perl script. this perl script calls a shell script.. after the shell script ends its execution, i am using exit 0 to terminate the shell script successfully and exit 1 to terminate the... (3 Replies)
Discussion started by: Little
3 Replies

10. UNIX for Advanced & Expert Users

Exit when sql script give error

Hi Guys, I am calling a SQL script which runs under while loop, i need to exit the while loop if i get error in sql script which is called while do sqlplus -s user/pass@db @test.sql id$i done test.sql whenever sqlerror exit; alter table t1 add &1 number; I need to come out of... (2 Replies)
Discussion started by: rohit_shinez
2 Replies
SCRIPT(1)						    BSD General Commands Manual 						 SCRIPT(1)

NAME
script -- make typescript of terminal session SYNOPSIS
script [-a] [-c COMMAND] [-e] [-f] [-q] [-t] [file] DESCRIPTION
Script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1). If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the file typescript. Options: -a Append the output to file or typescript, retaining the prior contents. -c COMMAND Run the COMMAND rather than an interactive shell. This makes it easy for a script to capture the output of a program that behaves differently when its stdout is not a tty. -e Return the exit code of the child process. Uses the same format as bash termination on signal termination exit code is 128+n. -f Flush output after each write. This is nice for telecooperation: One person does `mkfifo foo; script -f foo' and another can super- vise real-time what is being done using `cat foo'. -q Be quiet. -t Output timing data to standard error. This data contains two fields, separated by a space. The first field indicates how much time elapsed since the previous output. The second field indicates how many characters were output this time. This information can be used to replay typescripts with realistic typing and output delays. The script ends when the forked shell exits (a control-D to exit the Bourne shell (sh(1)), and exit, logout or control-d (if ignoreeof is not set) for the C-shell, csh(1)). Certain interactive commands, such as vi(1), create garbage in the typescript file. Script works best with commands that do not manipulate the screen, the results are meant to emulate a hardcopy terminal. ENVIRONMENT
The following environment variable is utilized by script: SHELL If the variable SHELL exists, the shell forked by script will be that shell. If SHELL is not set, the Bourne shell is assumed. (Most shells set this variable automatically). SEE ALSO
csh(1) (for the history mechanism), scriptreplay(1). HISTORY
The script command appeared in 3.0BSD. BUGS
Script places everything in the log file, including linefeeds and backspaces. This is not what the naive user expects. AVAILABILITY
The script command is part of the util-linux package and is available from ftp://ftp.kernel.org/pub/linux/utils/util-linux/. Linux July 30, 2000 Linux
All times are GMT -4. The time now is 07:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy