linuxnoob
linuxnoob

Reputation: 685

How do I add IF ELSE to linux variable

I have a variable in my script that displays the user:

OWNER=$(grep 1000:1000: /etc/passwd|cut -f1 -d':')

Output will be something like:

joe

This is fine on a normal server where the os was installed & user was setup during the install. I run into problems on a vps where there is no initial user. I would like to add to the variable IF blank then the output should be "root" but I don't know where to start..

Thankyou

Upvotes: 1

Views: 93

Answers (2)

Michael
Michael

Reputation: 1503

Why not to check for return value of grep command? If it returns 1 when use root as user, otherwise - cut its result and use it.

LINE="`grep 1000:1000: /etc/passwd`"
[ $? -eq 0 ] && OWNER="`echo "$LINE"|cut -f1 -d":"`" || OWNER=root

Upvotes: 1

Reinstate Monica Please
Reinstate Monica Please

Reputation: 11593

Something like this

[[ $OWNER == "" ]] && OWNER="root"

or

if [[ $OWNER == "" ]]; then
  OWNER="root"
fi

Upvotes: 4

Related Questions