elwayisgod
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 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/|gre p 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.
The command that runs in the scheduling tool is:
if [[ -n `ls /data/SAP/extract/MML/|grep sapgl.dat` ]]; then FILE_FOUND; fi
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/|gre
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That exits with 0 in Autosys for some reason so the job is success :(
std_err_file=/logs/autosys /$AUTO_JOB _NAME.$AUT ORUN.trace log
command=autosyslog -J TDC_SAPGL_EXTRACT-SAPGL -r 0 -n 0 -t E
size=0
========================== =========
========================== =========
std_err_file=/logs/autosys
command=autosyslog -J TDC_SAPGL_EXTRACT-SAPGL -r 0 -n 0 -t E
size=0
==========================
==========================
ASKER
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;
ASKER
I'm stumped. Not sure it can run in Autosys.
I execute this script from Autosys and it works:
I put that script on the command line in Autosys and if throws errors:
I execute this script from Autosys and it works:
if [[ -f "/data/SAP/extract/MML/sapgl.dat" ]]; then; exit 1; fi; exit 0;
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;'
===================================
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
We use Korn shell as default, I believe.
The main problem is: exec if [[ -f "/data/SAP/extract/MML/sap gl.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.
And a conditional statment on a shell commandline....
Please enter this command set in a script and call the script fro your software.
ASKER
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.
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.
ASKER
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...
Appreciate help...
ASKER
Open in new window
orOpen in new window
Am I on to something with 'test' or is that not valid?