bash - Getting errors trying to run shell script (Linux)
2014-07
I'm trying to run the following script. I have run it before without issue, but now I encounter an error.
#!/bin/bash
# init
function pause(){
read -p "$*"
}
echo & echo "(Website):" &&read input
output=$(ping -c 1 "$input" 2>/dev/null)
if [ $? -eq 0 ]; then
ip=$(printf '%s' "$output" | gawk -F'[()]' '/PING/{print $2}' 2>/dev/null)
clear
echo
echo "$ip";
echo -n $ip | xclip -selection c
echo
echo
echo IP copied to clipboard.
echo
echo && sleep 2
pause 'Press [Enter] key to exit...'
exit
else
clear
echo
echo "Host not found";
echo && sleep 1
pause 'Press [Enter] key to exit...'
exit
fi
But now all of a sudden it results in the following error:
3: /home/username/Desktop/shell.sh: Syntax error: "(" unexpected
I have not made any changes to the script itself, and I don't understand why this bracket results in an error now. Does anybody see anything in this script that I have missed?
Bash version: 4.2.45(1)-release
fresh install, mint 16 (updated everything)
EDIT: I'll put this here so I can actually show what happens.
I ended up removing the pause function completely:
#!/bin/bash
# init
echo & echo "(Website):" && read input
output=$(ping -c 1 "$input" 2>/dev/null)
if [ $? -eq 0 ]; then
ip=$(printf '%s' "$output" | gawk -F'[()]' '/PING/{print $2}' 2>/dev/null)
clear
echo
echo "$ip";
echo -n $ip | xclip -selection c
echo
echo
echo IP copied to clipboard.
echo
echo && sleep 2
exit
else
clear
echo
echo "Host not found";
echo && sleep 1
exit
fi
But when I run "sh ~/Desktop/shell.sh" I get this:
: not found/Desktop/shell.sh: 3: /home/username/Desktop/shell.sh:
(Website):
(I type google.com and press enter) and it returns
: bad variable name/shell.sh: 4: read:
: not found/Desktop/shell.sh: 5: /home/username/Desktop/shell.sh:
/home/username/Desktop/shell.sh: 24: /home/username/Desktop/shell.sh: Syntax error: word unexpected
Thank you all for the replies, is there something wrong with the shell itself?
I take back one of the previous version of your script. It's not needed to erase the function.
When I run the script with \bin\bash MyScript.sh
seems to function properly.
If instead I run it with \bin\sh MyScript.sh
it give me errors.
Why?
In many distributions \bin\sh
is a symlink to dash
, in other to bash
, in other can be the original sh
.
If you change with chown u+x MyScript.sh
the permission of the script and after you will execute it with ./MyScript.sh
the system will use the first line to understand with which shell
execute it. You can read more about the shebang e.g. here
If instead you run the script with sh MyScript.sh
you force the script to be executed with a different shell (in this case /bin/sh
).
#!/bin/bash
function pause(){
read -p "$*"
}
printf "\n(Website):" &&read input
output=$(ping -c 1 "$input" 2>/dev/null)
if [ $? -eq 0 ]; then
ip=$(printf '%s' "$output" | gawk -F'[()]' '/PING/{print $2}' 2>/dev/null)
clear
printf "\n${ip}";
echo -n $ip | xclip -selection c
printf "\n\nIP copied to clipboard.\n\n\n"
sleep 2
pause 'Press [Enter] key to exit...'
exit 0
else
clear
printf "\n\n Host not found\n\n"
sleep 1
pause 'Press [Enter] key to exit...'
exit 1
fi
Note:
I change many echo
with printf
, because in this case it is more compact.
With printf
you have to remember to put \n
to have a newline, \t
for a tab and so on...
Remove the two parentheses ()
from the definition of your function, so it looks like this:
function pause {
…
}
And this error:
bad variable name/hey.sh: 4: read: Syntax error: word unexpected
Is because of this:
&&read input
That should be like this:
&& read input
Just tried this with bash 4.2.47-2 on fedora linux 20 64 bits.
I'm trying to create a scheduled task that runs a shell script recurrently, and I'm having some trouble getting it to work. I give it the following command to run every minute:
~/Desktop/foo/my_script
But it doesn't ever run. (This command runs the shell script through the terminal no problem.) Any ideas what I'm doing wrong? Thanks!
Note: Here's my shell script:
#!/bin/bash
sleep 15
date >> output.txt
{ time ./foo > /dev/null ; } 2>> output.txt
And here's the cron line:
* * * * * /home/joe/Desktop/foo/my_script # JOB_ID_3
The scheduler most likely doesn't know how to expand ~
try giving it an absolute path instead.
EDIT, after solution found:
Another idea I had was that maybe cron was ignoring the line due to all *'s, but I couldn't replicate this. I did find that it's man page is rather unhelpful, but that wikipedia's page on cron is somewhat useful. I was going to suggest using the line:
*/1 * * * * /home/joe/Desktop/foo/my_script # JOB_ID_3
as this would run at */1
(ie every minute that divides by 1) if it continued to fail.
I figured out the problem. Cron runs the tasks in the home directory, so that's where my output file is showing up. This is why I thought it wasn't running.