# 'echo' in 'if' block not working



## ven (Aug 15, 2012)

Hi all,
I am very new to shell scripting. I wrote this simple script to check if the user is root:

```
#!/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:

```
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.


----------



## ven (Aug 15, 2012)

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:

```
#!/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:

```
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.


----------



## graudeejs (Aug 15, 2012)

You forgot to add $ to curr_user


```
[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*


----------



## graudeejs (Aug 15, 2012)

Here's the tutorial:
http://www.grymoire.com/Unix/Sh.html


----------



## UNIXgod (Aug 15, 2012)

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:


```
[ `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 (Aug 16, 2012)

Thanks a lot for the replies. Solved.

Regards,
$ven


----------



## anomie (Aug 16, 2012)

@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: 

 Quote string comparisons

```
[ "$val" = "something" ]
```

 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)
 Use sh(1)'s debugging option (set -x) to troubleshoot these types of problems


----------



## graudeejs (Aug 16, 2012)

This reminds of
EPIC Bug


----------



## ven (Aug 17, 2012)

Thanks anomie.
I love this forum.


----------

