Find Bash Syntax Error
Contents |
of Contents Debugging a script Use a unique name for your script Read the error messages Use a good editor Write logfiles Inject debugging code Use shell debug output Simple example of how to interpret xtrace output Making xtrace syntax error near unexpected token bash function more useful Debugging commands depending on a set variable Dry-run STDIN driven commands Common error
Syntax Error Near Unexpected Token Python
messages Unexpected end of file Unexpected end of file while looking for matching ... Too many arguments !": event not found syntax error syntax error near unexpected token shell script near unexpected token `(' The CRLF issue What is the CRLF issue? How did a CR end up in my file? Why do CRs hurt? How can I find and eliminate them? See also Discussion Debugging a bash syntax error near unexpected token (' ubuntu script These few lines are not intended as a full-fledged debugging tutorial, but as hints and comments about debugging a Bash script. Use a unique name for your script Do not name your script test, for example! Why? test is the name of a UNIX®-command, and most likely built into your shell (it's a built-in in Bash) - so you won't be able to run a script with the name test in a normal
Syntax Error Near Unexpected Token Mac
way. Don't laugh! This is a classic mistake Read the error messages Many people come into IRC and ask something like "Why does my script fail? I get an error!". And when you ask them what the error message is, they don't even know. Beautiful. Reading and interpreting error messages is 50% of your job as debugger! Error messages actually mean something. At the very least, they can give you hints as to where to start debugging. READ YOUR ERROR MESSAGES! You may ask yourself why is this mentioned as debugging tip? Well, you would be surprised how many shell users ignore the text of error messages! When I find some time, I'll paste 2 or 3 IRC log-snips here, just to show you that annoying fact. Use a good editor Your choice of editor is a matter of personal preference, but one with Bash syntax highlighting is highly recommended! Syntax highlighting helps you see (you guessed it) syntax errors, such as unclosed quotes and braces, typos, etc. From my personal experience, I can suggest vim or GNU emacs. Write logfiles For more complex scripts, it's useful to write to a log file, or to the system log. Nobody can debug your script without knowing what actually happened and what went wrong. An available syslog interface is logger ( online manpage). Injec
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 bash syntax error near unexpected token r Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags bash syntax error near unexpected token done' Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and
Syntax Error Near Unexpected Token Perl
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 voted up and rise to the top Shell script fails: http://wiki.bash-hackers.org/scripting/debuggingtips Syntax error: “(” unexpected up vote 31 down vote favorite 7 I've been 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 http://unix.stackexchange.com/questions/45781/shell-script-fails-syntax-error-unexpected sudo's time-out value by default. So I started experimenting by removing all the sudo lines and running the whole script 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 371k696751126 asked Aug 18 '12 at 11:37 kemra102 4731613 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 scrip
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss http://unix.stackexchange.com/questions/50456/syntax-error-in-a-bash-script-that-calls-find the workings and policies of this site About Us Learn more about http://unix.stackexchange.com/questions/133972/bash-syntax-error-when-else-follows-an-empty-then-clause Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. syntax error 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 in a bash script that calls find up vote 1 down vote favorite Where is the error in this script please: #!/bin/bash rep="git" files=`find' ${rep} '-type syntax error near f` for f in ${files} do echo $f done When i run find git -type f alone in the shell, it works! bash find quoting share|improve this question edited Oct 11 '12 at 0:37 Gilles 371k696751126 asked Oct 10 '12 at 10:29 user15992 161239 add a comment| 4 Answers 4 active oldest votes up vote 1 down vote accepted Remove quotes from files=`find '$rep' -type f` The correct script is #!/bin/bash rep="git" files=`find $rep -type f` for f in ${files}; do echo $f done share|improve this answer answered Oct 10 '12 at 10:49 Manula Waidyanatha 1,328810 2 Supposing you have no file names containing IFS characters. –manatwork Oct 10 '12 at 10:53 add a comment| up vote 6 down vote Strings in single quotes are not interpolated. It means, you are trying to run find ' ${rep} '-type f Remove the single quotes. If you really need to quote the $rep (e.g. because it contains spaces), use double quotes: files=`find "$rep" -type f` Note that there are no spaces inside the double quotes. You are searching '
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 Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & 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 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 Bash syntax error when “else” follows an empty “then” clause up vote 15 down vote favorite 5 Why would following script not execute, but give a syntax error of else: LOGS3_DIR=~/logs if [ -d "$LOGS3_DIR" ]; then cd cd "$LOGS3_DIR" echo "$LOGS3_DIR" for filename in `find "." -mtime 1 -type f` do if lsof "$filename" > /dev/null then # file is open else echo "deleting $filename" rm "$filename" fi done fi shell shell-script share|improve this question edited Jun 1 '14 at 22:59 Gilles 371k696751126 asked Jun 1 '14 at 19:05 Novice User 3803616 add a comment| 4 Answers 4 active oldest votes up vote 15 down vote accepted Don't use command substitution on the output of find. Here, everything can be done with find. find . -mtime 1 -type f ! -exec lsof -t {} \; -exec rm -f {} \; > /dev/null With GNU find, you can use -delete instead of -exec rm.... share|improve this answer answered Jun 1 '14 at 20:13 Stéphane Chazelas 178k28285511 13 While this is a much better way to do what @Novice User is attempting, it doesn't answer the question at all. –Chris J. Breisch Jun 2 '14 at 18:49 While -exec is often useful, as is xargs, sometimes a shell loop is needed. In which case a while read name loop is the preferred option (in bash with GNU find you can use the -0 option for both; portably you have to give up on newline). –Jan Hudec Jun 3 '14 at 8:06 @JanHudec, There are ways portably. -print0 is -exec printf '%s\0' {} + (but portably you can't deal with that output except if you want to consider perl), and with find .//. and some post-processing, you can escape the newlines for xargs. Note that