Sh vs ./


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sh vs ./
# 1  
Old 07-01-2013
Sh vs ./

Hi guys,

I have a small script which reads my IP address from an URL and then sets it :

Code:
#!/bin/bash
MAC_ADDR=$(ifconfig eth0 | sed -n 's/.*HWaddr \([a-f0-9:]*\).*/\1/p')
IP=($(curl http://169.254.169.254/latest/meta-data/network/interfaces/macs/$MAC_ADDR/local-ipv4s))
for ip in ${IP[@]:1}; do
echo "Adding IP: $ip"
    ip addr add dev eth0 $ip/24
done


I saved it in a file script.sh with exec rights. Now I am trying to put it under /etc/rc.local to run at startup.

My rc.local looks like :

Code:
root@ip-10-0-0-184:~# cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
sh /root/script.sh
exit 0

When I run it via ./script.sh all works fine. When I try to run it via sh I get the following
Code:
/root/script.sh: 3: /root/script.sh: Syntax error: "(" unexpected

Do you have any insights on how can I fix that ?

Last edited by jim mcnamara; 07-01-2013 at 01:42 PM.. Reason: code tags
# 2  
Old 07-01-2013
That error comes from using a bourne shell - are you on Solaris, or more correctly what shell does /bin/sh invoke?

Please show the output of:
Code:
ls -l /bin/sh
uname -a

# 3  
Old 07-01-2013
Code:
lrwxrwxrwx 1 root root 4 Mar 29  2012 /bin/sh -> dash

Linux ip-10-37-161-45 3.2.0-40-virtual #64-Ubuntu SMP Mon Mar 25 21:42:18 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux


Last edited by Scott; 07-01-2013 at 02:25 PM.. Reason: Code tags
# 4  
Old 07-01-2013
FWIW - https://bugs.launchpad.net/ubuntu/+s...sh/+bug/141481

dash is supposed to be POSIX-compliant but it still seems that it does not have all of the features that bash has, the $( ) construct is an example. You need to use backticks

Example:
Code:
`my command`
# instead of
$( my command)

PS: I don't use dash, but you should be aware of the gotchas if you choose to keep using it. dash is not meant as a one-to-one replacement for bash.
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 07-01-2013
Thanks, it was really helpful.
I guess cd /root && ./script.sh will also right straight from rc.local ...right ?
# 6  
Old 07-01-2013
Quote:
Originally Posted by jim mcnamara
dash is supposed to be POSIX-compliant but it still seems that it does not have all of the features that bash has, the $( ) construct is an example. You need to use backticks
According to its documentation, dash does support the modern, POSIX-compliant $(...) command substitution syntax.

I don't use dash either, but I suspect the problem lies with the parentheses surrounding the command substitution, which look like a bash list/array assignment.

Regards,
Alister
# 7  
Old 07-01-2013
What would be the fastest way to sort this out ?
Tried actually the following :

Code:
#!/bin/bash MAC_ADDR=`ifconfig eth0 | sed -n 's/.*HWaddr \([a-f0-9:]*\).*/\1/p'` IP=`(curl http://169.254.169.254/latest/meta-data/network/interfaces/macs/$MAC_ADDR/local-ipv4s)` for ip in ${IP[@]:1}; do echo "Adding IP: $ip"     ip addr add dev eth0 $ip/24 done

But still getting an error..

Last edited by liviusbr; 07-01-2013 at 03:44 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question