There's no command in the
else part for the script to execute. That will probably result in an error. Indeed,
bash doesn't allow you to have such empty fields at all, erroring out as soon as the script is read, so you must be using some other shell. In any case, if there's no command to run, just eliminate the
else keyword entirely.
If you aren't using a standard Linux and
bash, be sure to specify the environment you're using. It affects the advice we can give you.
It also helps to supply examples of what the inputs and desired outputs look like. I have no idea what the format would be for the desired line from
ps, or what characters 15-20 are. You could probably configure
ps to print out exactly what you want directly, with the proper options.
Now for some more comments on the scriptlet itself:
psHistory is not a command, but a file, correct? So don't put it in
$() command substitution brackets. If those are meant to be
${..} variable brackets, you'd be better off just removing them entirely. The full bracketed form of a variable shouldn't be used except with substitutions or in relatively rare concatenating circumstances. It otherwise does nothing but clutter up the code and make this kind of error more likely.
Your filter for
ps uses three separate processes when a single
awk expression can do the whole thing more efficiently.
Your
date command is wrong. There is no "$s" variable. I think you want
date +%s instead, and properly-formatted substitution brackets.
Code:
psHistory=/acc0/pshistory.log
killHistory=/acc0/killHistory.log
emailMessage=/acc0/emailMessage.txt
secondsToWait=600
emailDistro='name@domain.com,name1@domain.com',
##
###If file doesn't exist then create it.
##
if [ -f "$psHistory" ]; then
ps -ef | awk '/^lds/ && /LVEX/ { print substr($0,15,6) }' |\
while read pidSpec; do
echo "$(date '+%s') $pidSpec" "$psHistory"
done
fi
If it's possible to code for
/bin/bash (or
ksh) specifically, instead of the posix-only
/bin/sh, then we could certainly make it even cleaner, with
process substitution, double-bracket tests, and more advanced
parameter substitutions available to help us out.