Link to home
Start Free TrialLog in
Avatar of elwayisgod
elwayisgodFlag for United States of America

asked on

Unix Shell script in to look for a file and exit 1 if exists, otherwise exit 0

I have a script that we are converting to Autosys from a home grown scheduling tool.

The command that runs in the scheduling tool is:

if [[ -n `ls  /data/SAP/extract/MML/|grep sapgl.dat` ]]; then FILE_FOUND; fi

Open in new window


If I run that in Autosys I get error:
sh: line 2: syntax error near unexpected token `then'
sh: line 2: `exec if [[ -n `ls /data/SAP/extract/MML/|grep sapgl.dat` ]]; then FILE_FOUND; fi'

I tried replacing the 'then FILE_FOUND' with 'exit 1' and still doesn't work.  

Basically if the file 'sapgl.dat' exists in that directory I need to 'exit 1' so Autosys fails.  If it doesn't then it's success and process can continue.   I think in Autosys I need it on a single line of code.  I think the ; are line breaks but not completely sure.

Any guidance is appreciated.
Avatar of elwayisgod
elwayisgod
Flag of United States of America image

ASKER

I found a 'test' command.  Not sure this will work as everytime I run it, it's a success.  I'm using the -s param and -e and always success even when the file is there.....

test -e /data/SAP/extract/MML/sapgl.dat && exit 1 || exit 0

Open in new window

or
test -s /data/SAP/extract/MML/sapgl.dat && exit 1 || exit 0

Open in new window


Am I on to something with 'test' or is that not valid?
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That exits with 0 in Autosys for some reason so the job is success :(

std_err_file=/logs/autosys/$AUTO_JOB_NAME.$AUTORUN.tracelog
command=autosyslog -J TDC_SAPGL_EXTRACT-SAPGL -r 0 -n 0 -t E
size=0
===================================

===================================
I wrote this script and it works, but is there a way to make this a one line script so I don't have to run script outside Autosys?

#!/usr/bin/ksh
#set up variables

file="/data/SAP/extract/MML/sapgl.dat"
if [[ -f "$file" ]]; then
	exit 1;
else
	echo "No sapgl.dat file found. Process can continue!!"
fi

# exit with successful exit code
exit 0;

Open in new window

I'm stumped.  Not sure it can run in Autosys.

I execute this script from Autosys and it works:
if [[ -f "/data/SAP/extract/MML/sapgl.dat" ]]; then; exit 1; fi; exit 0;

Open in new window


I put that script on the command line in Autosys and if throws errors:
std_err_file=/logs/autosys/$AUTO_JOB_NAME.$AUTORUN.tracelog
command=autosyslog -J TDC_SAPGL_EXTRACT-SAPGL -r 38973045 -n 73 -t E
size=429
===================================
/opt/CA/WorkloadAutomationAE/SystemAgent/server1/spool/TST_SCH/MAIN/WAAE_WF0.1/115313.38973045_73.59A95D3CE4A2B9169F0030AD7F8D42252C422C5C.sh: line 2: syntax error near unexpected token `then'
/opt/CA/WorkloadAutomationAE/SystemAgent/server1/spool/TST_SCH/MAIN/WAAE_WF0.1/115313.38973045_73.59A95D3CE4A2B9169F0030AD7F8D42252C422C5C.sh: line 2: `exec if [[ -f "/data/SAP/extract/MML/sapgl.dat" ]]; then; exit 1; fi; exit 0;'

===================================

Open in new window

The error is telling you that you incorrectly put then in the section.  Autosys must be expecting something else.  Maybe there's an extra semicolon?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
We use Korn shell as default, I believe.
Avatar of noci
noci

The main problem is: exec if [[ -f "/data/SAP/extract/MML/sapgl.dat" ]]; then exit 1; fi; exit 0;
And a conditional statment on a shell commandline....

Please enter this command set in a script and call the script fro your software.
Yes.  I can create the .ksh script and just call the script in Autosys and it works perfectly.  Was just trying/wondering if I can run on command line because Autosys allows to run commands directly.  That way I don't have to worry about someone deleting a script or maintaining it in future.  That's all.
only if   a bare:     test ! -e filename
could work, exec would not handle expressions very well on it's own, and a one line command cannot easily be interpreted.
! = not
-e = exist (-f  = Exits AND is normal file, likewise -d = Exist AND is directory)  So a not  -e is better in this case.
Thanks for assistance.  Went with separate korn shell script and just execute that via Autosys.  Gave up on trying to get this on a single line in Autosys :)

Appreciate help...