sed command

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.

read loginID
/usr/ucb/sed '/^'$loginID'/d' /user/file1 > /tmp/tmp1
mv /tmp/tmp1 /usr/file1

My problem is if user just press enter key instead of key in valid ID, the tmp1 will empty and it will erease file1.

How to make sure loginID is not nothing?

Thanks for your help!

For one thing, test if loginID is an empty string right after your read command. There are several ways to do this.

if [ "$loginID" != "" ]
then
   run your sed command
fi

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

Shell program can not see variable $loginID when user just press enter key.
the
if [ "$loginID" != "" ]

statement will be like this:
+ [ != ]
test: argument expected
.....

???

$ loginID=
$ if [ "$loginID" != "" ] ; then echo nonblank ; else echo blank ; fi
blank
$ loginID=xyz
$ if [ "$loginID" != "" ] ; then echo nonblank ; else echo blank ; fi
nonblank
$

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.

works just fine under Solaris' Bourne:

#!/bin/sh

read loginID
if [ "$loginID" != "" ] ; then echo nonblank ; else echo blank ; fi

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.