'echo' in 'if' block not working

Hi all,
I am very new to shell scripting. I wrote this simple script to check if the user is root:
Code:
#!/bin/bash

curr_user=`whoami`

ls -l
if [ curr_user = 'root' ]
then
        echo 'you are root'
        ls -l
        echo 'you are root 2'
else
        echo "you are $curr_user."
        echo "you are $curr_user 2."
fi

exit
And here's the output:
Code:
vc@linux-3o76:~/work> ./a.sh
total 4
-rwxrwxrwx 1 vc users 195 Aug 15 09:21 a.sh
you are vc.
you are vc 2.
vc@linux-3o76:~/work> sudo ./a.sh
total 4
-rwxrwxrwx 1 vc users 195 Aug 15 09:21 a.sh
you are root.
you are root 2.
vc@linux-3o76:~/work>
Why isn't the "ls -l" not working in the "if" block? Where am I going wrong? Has it got something to do with the syntax of the if block?

Regards,
Ven.
 
Hi all,
I somehow (closing the terminal and reopening it) got that ls -l to work in the "if" block. But what's happening now is even more strange.
This is my script:
Code:
#!/bin/bash

curr_user=`whoami`

ls -l
if [ curr_user = 'root' ]
then
        echo 'you are root'
        hciconfig hci0 up
        hcitool -i hci0 cc --role=s 00:23:F1:F1:7F:D5
        echo 'you are root 2'
else
        echo "you are $curr_user."
        ls -l
        echo "you are $curr_user 2."
fi

exit
Output:
Code:
vc@linux-3o76:~/work> ./a.sh
total 4
-rwxrwxrwx 1 vc users 261 Aug 15 09:53 a.sh
you are vc.
total 4
-rwxrwxrwx 1 vc users 261 Aug 15 09:53 a.sh
you are vc 2.
vc@linux-3o76:~/work> sudo ./a.sh
root's password:
total 4
-rwxrwxrwx 1 vc users 261 Aug 15 09:53 a.sh
you are root.
total 4
-rwxrwxrwx 1 vc users 261 Aug 15 09:53 a.sh
you are root 2.
That's what I got after restarting my machine. Why is still the old script being executed? What is going on here? <insert at-wit's-end smiley>
My bash version: GNU bash, version 4.2.10(1)-release (i586-suse-linux-gnu)
Thanks in advance.
Regards,
Ven.
 
You forgot to add $ to curr_user

Code:
[color="red"]#!/bin/bash[/color]

curr_user=`whoami`

ls -l
if [ [color="Red"]$[/color]curr_user = 'root' ]
then
        echo 'you are root'
        ls -l
        echo 'you are root 2'
else
        echo "you are $curr_user."
        echo "you are $curr_user 2."
fi

exit

also /bin/bash is non-standard shell (and bash is in different place on FreeBSD [if installed at all])
standard shell is /bin/sh
 
For one don't use bash ... which is not located in /bin. You use /bin/sh in your hash-bang line.

to test for root you can use:

Code:
[ `id -u` -ne 0 ]

For the sake of portability it's best to either use POSIX /bin/sh or when a language or other shell is required use (ruby in this example) #!/usr/bin/env ruby

Happy Hacking!
 
@ven, although your thread is marked solved, please carefully read the feedback from the folks who replied.

In addition to using /bin/sh (you are on a FreeBSD forum, so presumably you care about portability) as your interpreter, also:
  1. Quote string comparisons
    Code:
    [ "$val" = "something" ]
  2. Provide a sane, local PATH value in your script (so that it's not inherited from the environment and running some strange commands you didn't expect)
  3. Use sh(1)'s debugging option (set -x) to troubleshoot these types of problems
 
Back
Top