Command works when pasted at command line but not as bash script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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,
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
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.
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:
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.