I'm pretty new to bash and am having trouble with what I assume is formatting. I'm trying to edit the /etc/profile so it will display a login message for root and a different login message for anyone else. But I'm getting the error bash: syntax error near unexpected token else. I've tried all the different combinations of no semicolon, then on the next line etc but always get the same error. I've tried the lines separately and they display fine (except $HOSTNAME, can't get that to work). When run like this and login with root it will just jump to "Welcome $USER...".

Anyone suggestions would be appreciated!

if [ "$UID" -ne 0 ]; then
  echo -e "\033[40;37;7m Danger! Root is doing stuff in `pwd`\033[0m"
else
  echo "Welcome $USER! You are working on `$HOSTNAME` in `pwd`."
fi

没有正确的解决方案

其他提示

As written, above works for me - but, as pointed out, you should change your test to -eq 0.

For the syntax error near unexpected token problem - I will guess that your file contains embedded 'control codes', i.e. most likely a carriage return \r.

Try:

cat -e ~/your_profile

see any non-printable characters? if so, remove them (cat options may vary - check you manpage) or

od -c ~/your_profile
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top