I'm trying to send the file list as parameter to another job and execute it.
But the loop doesn't work, the inner job is running only once and not twice as expected
for filelist in $(ls -rt *.txt | tail -2)
do
echo $filelist
export filelist
cmd="$Program -config $configfile -autoexec $autoexecfile -log $LogFile -sysin $SourceDir/Load.sas"
done
At least the "echo" statement should be executed, no?
At first, make shure there are indeed at least 2 files fitting the globbing expression in the PWD. (In case you have overseen it: the "ls" uses no path, so it works in the current directory.)
Second: you should use "tail -n 2" instead of "tail -2".
Good catch: if some filenames contain control characters the original construct with the subshell "$(...)" might result in somewhat obfuscated output because this is interpreted by the shell a second time.
It will be a good idea to test which output ls -rt *txt | tail -n 2 really produces.
We are running a SAS job whose input table name changes every time so to solve this we are exporting this as a UNIX variable and inside the SAS job convert again to a SAS variable .
This works fine if the list contains only one variable.
You are not using $filelist in your cmd definition, so - if at all - that one identical command is executed twice. And, $filelist will hold one single file name per loop, so the name may be misleading. Why don't you deploy Yoda's advice and post the output of your script executed with xtrace/verbose options?
What is wrong with running it just as $cmd - there's nothing to evaluate, and $Program (sas) is already executable. And why store the command ($cmd) in a variable at all?