Hi,
I have shell program read a user input and then I need to write an if statement to see if it matches in a file and if matches delete it from the file.
if [[ $loginID != "" ]]
then
/usr/ucb/sed '/^'$loginID'/d' /user/file1 > /tmp/tmp1
mv /tmp/tmp1 /usr/file1
fi
or
/usr/ucb/sed '/^'$loginID'/d' /user/file1 > /tmp/tmp1
if [[ ! -s /tmp/tmp1 ]]
then
mv /tmp/tmp1 /usr/file1
fi
Perderabo,
What's your point?
The issue is the if statement will not pass syntax error checking.
if [ "$loginID" != "" ]...
The rest will not run.
loginID is passed by read command.
Run the example with trace turned on exactly as listed and you will see what you are missing. The quotations are key. "$loginID" is expanded to "" and "" matches "".
Other alternatives can be used with [[ ]] and with ${#loginID} (ksh). Bourne supports the quotations as far back as I can remember.
I posted the output of what happens when I tried it. Notice that it worked. "Worked" means that it did indeed pass "syntax error checking". If YOU would try it, you would see that it works too. It is very tedious indeed when you refuse to try something and instead repeatedly assert that a correct solution will not work.