LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2012, 03:25 PM   #1
neild
LQ Newbie
 
Registered: Sep 2012
Posts: 2

Rep: Reputation: Disabled
Command works when pasted at command line but not as bash script


I want to run the following bash command at startup.

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 have copied the command from the website silkapp.com.

Basically it gets the secondary internal IP addresses allocated to an Amazon EC2 instance from some remote metadata and adds the IPs to eth0 on the machine.

It works fine when pasted into the command line.

However, if I paste it into /path/script.sh and run:
sh /path/script.sh

I get:

Syntax error: "(" unexpected

How do I get the script to run correctly?

I know pretty much nothing about bash scripts and I imagine this is obvious to some of you, but I am stumped.

Any help would be sincerely appreciated.
 
Old 09-20-2012, 08:46 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Could be a pasting issue.
Sometimes when pasting into eg vi, a wrapped line like that 'IP=..' will end up with a (invisible) newline char in the middle.
Its an interaction between vi and the clipboard/mouse.
Just go into he editor and make sure each cmd line has no newline char eg by cursoring along the line, it will stop at the newline char,
 
Old 09-20-2012, 09:54 PM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by neild View Post
I want to run the following bash command at startup.

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
Might help is code tags were used.
Code:
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
Quote:
Originally Posted by neild View Post
I have copied the command from the website silkapp.com.
depending on how the original code was rendered on your browser it may have some 'extra' stuff in the pasted clipboard contents.

What does
Code:
curl http://169.254.169.254/latest/meta-data/network/interfaces/macs/$MAC_ADDR/local-ipv4s
in a terminal kick out on this instance?
 
Old 09-20-2012, 11:30 PM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

The problem is that POSIX conformable shells (like /bin/sh -> dash on Debian-like systems) lack support for arrays. When you type these commands in command line, you actually use bash, an enhanced shell. Try to run your script as follows:
Code:
bash ./yourscript.sh
..And welcome to LQ!
 
1 members found this post helpful.
Old 09-21-2012, 12:33 AM   #5
Zero Angel
Member
 
Registered: Jul 2010
Distribution: PinguyOS 12.04
Posts: 50

Rep: Reputation: 1
Wouldnt
Code:
#!/bin/bash
on the first line do the same thing?
 
Old 09-21-2012, 12:59 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
no, if you specify the shell (like: sh <script>) the shebang will not be used
 
Old 09-21-2012, 06:03 AM   #7
neild
LQ Newbie
 
Registered: Sep 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks firstfire that is perfect.

Code:
bash ./yourscript.sh
makes the script work as expected. I didn't realise arrays were an additional feature of bash

Quote:
Might help is code tags were used.
Thanks, will bear in mind next time.

If anyone is interested

Code:
curl http://169.254.169.254/latest/meta-data/network/interfaces/macs/$MAC_ADDR/local-ipv4s
returns a line-break delimited list of IP addresses such as:
Code:
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
Quote:
welcome to LQ!
Thanks, sorry I am using first post to ask for help rather than help out.
 
Old 09-23-2012, 07:30 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by pan64 View Post
no, if you specify the shell (like: sh <script>) the shebang will not be used
Zero Angel almost certainly means the opposite. That is, if the OP ensures that the first line is #!/bin/bash(and not #!/bin/sh or some other executable), then the script will work properly when executed directly. The shebang controls which interpreter is used when the script file is executed as a stand-alone command, ignoring the system default.

In other words, there's no need to explicitly invoke a shell if you code the script properly.


See these links for this and other common scripting mistakes:

http://wiki.bash-hackers.org/scripting/newbie_traps
http://mywiki.wooledge.org/BashPitfalls
http://mywiki.wooledge.org/BashFAQ
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Command line works, won't work from inside bash script w6lsn Programming 4 02-17-2011 05:45 AM
(BASH) Works on the command line, hangs as a script -- what's wrong? SilversleevesX Programming 17 08-08-2010 10:19 PM
Bash script works from command line, fails from cron cmfarley19 Linux - General 4 08-14-2009 12:24 PM
works on command line but not in bash script tara Linux - General 7 02-09-2009 03:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration