Bash Shell Syntax Error Unexpected
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting shell syntax error unexpected end of file ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & syntax error unexpected shell script Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a linux syntax error unexpected end of file minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Shell script fails: Syntax error: “(” unexpected up vote 31 down vote favorite 7 I've been linux syntax error near unexpected token working on a script that automates setting up a development environment for Raspberry Pi development (step by step details that work are here). The script is linked in that article but convenience you can find it here also. Now when run this script install and sets up the environment without error but you have to enter your sudo password more than once due to sudo's time-out value by default. So I started experimenting by removing all the sudo lines and running the whole script
Linux Syntax Error Near Unexpected Token Then'
via sudo at the command line like so: kemra102@ubuntuvm:~$ sudo ./pi_dev_env_install.sh This works fine as expected and gets most of the way through until this point: ./pi_dev_env_install: 68: ./pi_dev_env_install.sh: Syntax error: "(" unexpected Now this line worked fine previously when not running the whole script with sudo. There is nothing about this line running as sudo that should stop it working to my knowledge, does anyone have any ideas? bash shell ubuntu shell-script share|improve this question edited Aug 18 '12 at 12:08 Gilles 369k666681119 asked Aug 18 '12 at 11:37 kemra102 4681613 The shebang is really in line 9? Due to Ubuntu's DashAsBinSh affinity I suspect your script is interpreted by dash instead of bash. Try to move the shebang in line 1. –manatwork Aug 18 '12 at 11:45 According to that article calling /bin/bash directly instead of /bin/sh will; correctly use bash instead of dash so that should not be an issue as I understand it. I can still move the shebang of course, but that doesn't really explain as to why it works when you don’t sudo the whole script. –kemra102 Aug 18 '12 at 11:50 add a comment| 5 Answers 5 active oldest votes up vote 47 down vote accepted The script does not begin with a shebang line, so the kernel executes it with /bin/sh. On Ubuntu, /bin/sh is dash, a shell designed for fast startup and execution with only standard features. When dash reaches line 68, it sees a syntax
Syntax error: "(" unexpected stigalaJuly 12th, 2007, 09:33 AMHi, I'm trying to run a script on ubuntu 7.04; stig@stig-laptop:~/mosesdecoder$ ./regenerate-makefiles.sh ./regenerate-makefiles.sh: 13: Syntax error: "(" unexpected but then I
Linux Syntax Error Near Unexpected Token Newline'
get the syntax error. Below is the first lines in the script, line syntax error ( unexpected bash array 13 in red. #!/bin/sh # NOTE: # Versions 1.9 (or higher) of aclocal and automake are required. # For Mac ksh syntax error unexpected OSX users: # Standard distribution usually includes versions 1.6. # Get versions 1.9 or higher # Set the following variable to the correct paths #ACLOCAL="/path/to/aclocal-1.9" #AUTOMAKE="/path/to/automake-1.9" function die () { echo "$@" http://unix.stackexchange.com/questions/45781/shell-script-fails-syntax-error-unexpected >&2 exit 1 } ... I already have automake (GNU automake 1.9.6) and aclocal (GNU automake 1.9.6). Setting the paths is only for Mac OSX users, so I didn't try to set any of those paths. Anyone can explain this? Thanks for any help, Stig Rui PaisJuly 12th, 2007, 10:01 AMHi. the (inexistent) problem is the 1st line in combination with Ubuntu. When you https://ubuntuforums.org/archive/index.php/t-499045.html run it using ./ it will use the shell you mention on first line, in your case: #!/bin/sh if you do a ls -l /bin/sh you will see that it's just a link to dash. Ubuntu, since Edgy, replaced old sh with dash, creating a series of incompatibilities in a lot of scripts... So, either you run: bash regenerate-makefiles.sh or replace first line with: #!/bin/bash (or even remove the 1st line and environment will call it with bash) or (losing generality) adapt it for dash: remove the keyword function: die() { ... } hth stigalaJuly 12th, 2007, 10:55 AMThanks a lot, Rui! I used bash regenerate-makefiles.sh and the script ran perfectly. I guess I'll have to read up on dash, bash and sh to understand the difference between the different shells. Stig Rui PaisJuly 12th, 2007, 11:16 AMNo prob :) sh and bash has more or less the same syntax, so usually no problem came from there... but dash is much more different. it's suppose to be much lighter and faster then bash, being that the reason why they choose it by Edgy days. A lot of people had problem, specially wit
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring http://stackoverflow.com/questions/6347119/bash-syntax-error-unexpected/6347145 developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join http://superuser.com/questions/802924/syntax-error-unexpected-in-bash-script them; it only takes a minute: Sign up bash Syntax error: “(” unexpected up vote 5 down vote favorite 1 For some reason this function is working properly, the terminal is outputting newbootstrap.sh: 2: Syntax error: "(" unexpected Here syntax error is my code (line 2 is function MoveToTarget() {) #!/bin/bash function MoveToTarget() { #This takes to 2 arguments: source and target cp -r -f "$1" "$2" rm -r -f "$1" } function WaitForProcessToEnd() { #This takes 1 argument. The PID to wait for #Unlike the AutoIt version, this sleeps 1 second while [ $(kill -0 "$1") ]; do sleep 1 done } function RunApplication() { #This takes 1 application, the path to the thing to execute exec "$1" } syntax error unexpected #our main code block pid="$1" SourcePath="$2" DestPath="$3" ToExecute="$4" WaitForProcessToEnd $pid MoveToTarget $SourcePath, $DestPath RunApplication $ToExecute exit linux osx bash scripting syntax-error share|improve this question edited Mar 2 at 20:59 Jens 36.2k863104 asked Jun 14 '11 at 16:50 rsmith 2613 add a comment| 2 Answers 2 active oldest votes up vote 17 down vote You're using the wrong syntax to declare functions. Use this instead: MoveToTarget() { # Function } Or this: function MoveToTarget { # function } But not both. Also, I see that later on you use commas to separate arguments (MoveToTarget $SourcePath, $DestPath). That is also a problem. bash uses spaces to separate arguments, not commas. Remove the comma and you should be golden. share|improve this answer edited Jun 14 '11 at 16:58 answered Jun 14 '11 at 16:52 Rafe Kettler 40.3k12104123 Thanks! It makes sense now. I saw some websites that did that. –rsmith Jun 14 '11 at 16:53 1 @rsmith that website was incorrect, then. –Rafe Kettler Jun 14 '11 at 16:53 @rsmith Name the site or it was a dream :-) –Jens Mar 2 at 21:00 add a comment| up vote 1 down vote I'm also new to defining functions in bash scripts. I'm using a bash of version 4.3.11(1):-release (x86_64-pc-linux-gnu) on Ubuntu 14.04. I don't know why but the definition that starts with the keyword function never works for me. A definition li
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Super User Questions Tags Users Badges Unanswered Ask Question _ Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Syntax error: “(” unexpected in bash script up vote -1 down vote favorite The script is for adding torrent files to a folder and having them added to transmission. Here's the script: #!/bin/bash for file in /home/me/box/*/* do dir=$(basename $(dirname "$file")); sudo chmod 0777 /var/log/torrentwatch.log sudo chmod -R 0777 /home/me/box/*/* if "$file" = "/home/me/box/*/*.torrent"; then echo [`date`] "$file" added to queue. >> /var/log/torrentwatch.log /usr/bin/transmission-remote localhost:9091 --auth=transmission:transmission -l -w /media/Media/Torrents/$dir -a "$file" sleep 40 && rm "$file" sleep 3 && sudo chmod -R 777 /media/Media && sudo chown -R debian-transmission:debian-transmission /media/Media/info fi done The problem is that when I run the script I get /home/me/box/TV/Name.of.file.torrent: Syntax error: "(" unexpected I've tried running the script with bash, sh, and zsh, and none seem to work. I can't figure out what the problem is. bash bash-scripting share|improve this question asked Aug 25 '14 at 23:06 user450632 32 Try prepending set -xv to the script. –choroba Aug 25 '14 at 23:12 stackoverflow.com/q/25495636/1030675 –choroba Aug 25 '14 at 23:15 add a comment| 1 Answer 1 active oldest votes up vote 0 down vote accepted There are several issues from what I see in your script. Try the following. Replace dir=$(basename $(dirname "$file")); With dir=$(basename "$(dirname "$file")"); This should resolve your error, however, there are other few things I recommend: echo [`date`] "$file" added to queue. >> /var/log/torrentwatch.log I would change that to echo "[$(date)]" "$file" added to queue. >> /var/log/torrentwatch.log This would prevent few issues, among them word s