Linux 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 syntax error ( unexpected bash array Stack Overflow the company Business Learn more about hiring developers or posting ads with
Syntax Error At Line 1 (' Unexpected In Shell Script
us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer
Syntax Error ( Unexpected Ubuntu
site for users of Linux, FreeBSD and other Un*x-like operating systems. 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
Ksh Syntax Error Unexpected
voted up and rise to the top `Syntax error: “(” unexpected` when creating an array up vote 1 down vote favorite 1 I have two (Debian) Linux servers. I am creating a shell script. On the first one I create an array thus: #!/bin/bash target_array=( "/home/user/direct/filename -p123 -r" ) That works fine. But when I run this on the other server I get: Syntax error: "(" unexpected As far as I can line 1: syntax error: unexpected word (expecting ")") tell both servers are the same. Can anyone shed some light on why this doesn't work? If I type it into the terminal directly it is fine?? It would appear that when I run it as sh scriptname.sh I get the error, but if I run it as ./scriptname.sh it seems to be ok. What's the difference? bash shell-script array share|improve this question edited Jan 7 at 23:36 Gilles 372k696761127 asked Jan 7 at 14:42 IGGt 398214 Did you copy-pasted the script between the two server? try cat -v script to see if there are spurious char. –LilloX Jan 7 at 14:50 2 sh is not bash and so running it as sh scriptname.sh is wrong –Eric Renouf Jan 7 at 15:01 add a comment| 3 Answers 3 active oldest votes up vote 7 down vote accepted When you use ./scriptname.sh it executes with /bin/bash as in the first line with #!. But when you use sh scriptname.sh it executes sh, not bash. The sh shell has no syntax to create arrays, but Bash has the syntax you used. share|improve this answer edited Jan 7 at 23:37 Gilles 372k696761127 answered Jan 7 at 15:06 Konstantin Morenko 2086 OK, that makes sense now. As you can probably tell I'm q
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 bash function syntax error unexpected company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions syntax error at line 1 (' unexpected solaris Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million sh: 1: syntax error: "(" unexpected python programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Syntax error: “(” unexpected (expecting “done”) [duplicate] up vote 2 down vote favorite 1 This question already has an answer here: http://unix.stackexchange.com/questions/253892/syntax-error-unexpected-when-creating-an-array I am getting error “array.sh: 3: array.sh: Syntax error: ”(“ unexpected” 3 answers I have a very simple shell script which I'm using to loop through directories, and call another shell script. I wrote it on my local machine (OSX running bash 3.2), and am using it on a remote server running bash 4.2. On the server, when I type which bash, I get /bin/bash, so I added the line on top. I http://stackoverflow.com/questions/29287474/syntax-error-unexpected-expecting-done still get this error, pointing to the line that begins arrIN=... 8: run_all_verification.sh: Syntax error: "(" unexpected (expecting "done") The shell script: #!/usr/bin/bash #base name for all experiments BASE_EXP_ID=$1; for i in ${BASE_EXP_ID}* do #split file name by "__" arrIN=(${i//__/ }); EXP_ID=${arrIN[0]} NUM_FEATURES=${arrIN[1]} echo "${EXP_ID} ${NUM_FEATURES}" sh run_verification.sh ${EXP_ID} ${NUM_FEATURES} done bash shell share|improve this question asked Mar 26 '15 at 19:43 Adam_G 1,20652654 marked as duplicate by tripleeebash Users with the bash badge can single-handedly close bash questions as duplicates and reopen them as needed. Dec 14 '15 at 8:32 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. 2 If you mean bash then run bash and not sh (for run_verification.sh). How are you running the posted script (which is run_all_verification.sh I'm assuming)? ./run_all_verification.sh? sh run_all_verification.sh? bash run_all_verification.sh? –Etan Reisner Mar 26 '15 at 19:53 add a comment| 1 Answer 1 active oldest votes up vote 6 down vote accepted Your error message is from dash, probably because you ran sh filename. To run a script with bash, use bash filename (or ./filename). share|improve this answer answered Mar 26 '15 at 19:54 that other guy 45.4k54070 I didn't realize there's a difference. On my local machine, and
& Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers Search Forums Show Threads Show http://www.unix.com/unix-for-dummies-questions-and-answers/3609-syntax-error-unexpected.html Posts Tag Search Advanced Search Unanswered Threads Find All Thanked Posts Go to Page... learn linux and unix commands - unix shell scripting syntax error: `(' unexpected UNIX for Dummies Questions & Answers Page 1 http://linuxcommand.org/wss0100.php of 2 1 2 > Thread Tools Search this Thread Display Modes #1 12-13-2001 adadevil Registered User Join Date: Jun 2001 Last Activity: 17 October 2004, 11:45 AM EDT Posts: 8 Thanks: 0 syntax error Thanked 0 Times in 0 Posts syntax error: `(' unexpected Hi All, I am trying to run an executable on Sun OS and it gives me following error: syntax error: `(' unexpected On other server this same executable is running fine. Shell on boht server is ksh. Please help! Thanks Remove advertisements Sponsored Links adadevil View Public Profile Find all posts by adadevil #2 12-13-2001 wizard Registered User Join Date: Nov syntax error unexpected 2001 Last Activity: 11 February 2002, 9:50 AM EST Posts: 60 Thanks: 0 Thanked 1 Time in 1 Post This usually means that the executable format of the binary does not match the executable format expected by the OS. In other words, the app was compiled on a different platform than the one you are trying to run it on. What OS is running on the machine where the app works? What OS is running on the machine where the app gives this message? And last, what is the output of the file command on each machine i.e. file executable_file_name Remove advertisements Sponsored Links wizard View Public Profile Find all posts by wizard #3 12-13-2001 rwb1959 Registered User Join Date: Aug 2001 Last Activity: 16 October 2009, 2:04 PM EDT Location: Virginia, USA Posts: 438 Thanks: 0 Thanked 1 Time in 1 Post I'm assuming this "program" is a Korn Shell script (???). On Sun OS, normally, the default shell is "csh". I would ask the following... Is the shell script installed as "executable" (i.e. rwxr-xr-x permissions)? Does it contain as the very 1st line: #!/bin/ksh (or whatever path to "ksh")? What do you get when you do... env | grep -i SHELL How do you actually execute the script? rwb1959 View Public Profile Visit rwb1959's homepage! Find
Out Of Trouble by William Shotts, Jr. Now that our scripts are getting a little more complicated, I want to point out some common mistakes that you might run into. To do this, create the following script called trouble.bash. Be sure to enter it exactly as written. #!/bin/bash number=1 if [ $number = "1" ]; then echo "Number equals 1" else echo "Number does not equal 1" fi When you run this script, it should output the line "Number equals 1" because, well, number equals 1. If you don't get the expected output, check your typing; you made a mistake. Empty variables Edit the script to change line 3 from: number=1 to: number= and run the script again. This time you should get the following: [me@linuxbox me]$ ./trouble.bash /trouble.bash: [: =: unary operator expected. Number does not equal 1 As you can see, bash displayed an error message when we ran the script. You probably think that by removing the "1" on line 3 it created a syntax error on line 3, but it didn't. Let's look at the error message again: ./trouble.bash: [: =: unary operator expected We can see that ./trouble.bash is reporting the error and the error has to do with "[". Remember that "[" is an abbreviation for the test shell builtin. From this we can determine that the error is occurring on line 5 not line 3. First, let me say there is nothing wrong with line 3. number= is perfectly good syntax. You will sometimes want to set a variable's value to nothing. You can confirm the validity of this by trying it on the command line: [me@linuxbox me]$ number= [me@linuxbox me]$ See, no error message. So what's wrong with line 5? It worked before. To understand this error, we have to see what the shell sees. Remember that the shell spends a lot of its life substituting text. In line 5, the shell substitutes the value of number where it sees $number. In our first try (when number=1), the shell substituted 1 for $number like so: if [ 1 = "1" ]; then However, when we set number to nothing (number=), the shell saw this after the substitution: if [ = "1" ]; then which is an error. It also explains the rest of the error message we received. The "=" is a binary operator; that is, it expects two items to operate upon - one on each side. What the shell was trying to tell us was that there was only one item and there should have been a unary operator (like "!") that only operates on a single item. To fix this problem, change line 5 to read: if [ "$number" = "1" ]; then Now when the shell performs the