Results 1 to 6 of 6
Hey all,
I'm new to bash programming and i'm writing a directory compare script that is giving me a syntax error since i've added some functions to it, and I ...
- 09-16-2008 #1Just Joined!
- Join Date
- Sep 2008
- Posts
- 6
Syntax error
Hey all,
I'm new to bash programming and i'm writing a directory compare script that is giving me a syntax error since i've added some functions to it, and I can't find the error - probably because of my lack of familiarity with bash. Here is my error output:
My code is:Code:Retrieving listing of dir: testdir : command not found 'ircmp: line 3: syntax error near unexpected token `{ 'ircmp: line 3: `ChkFile(){
Code:echo "Retrieving listing of dir:" $1 ChkFile(){ filename="$1" status=0 if [ ! -e "$filename" ] ; then #======================================== # Check fails if the file does not exist. #======================================== status=1; elif [ -d "$filename" ] ; then #============================================== # Check fails if a directory file does not have # both x (search) and r (read) permissions. #============================================== if [ ! -x "$filename" ] ; then status=2 elif [ ! -r "$filename" ] ; then status=3 fi elif [ -f "$filename" ] ; then #============================================== # Check fails if a regular file does not have # r (read) permission. #============================================== if [ ! -r "$filename" ] ; then status=4 fi else #============================================== # Check fails if the file is of another type. #============================================== status=5 fi return $status } GetLst(){ if [ "$1" = -r ] ; then maxDepth="" shift 1 else maxDepth="-maxdepth 1" fi #------------------------------------ # Get the list of files using "find". #------------------------------------ dirname="$1" cd "$dirname" 2>/dev/null list=`find . -mindepth 1 $maxDepth "(" -type d -o -type f ")" 2>/dev/null` #----------------------------------- # Find has "./" at the start of each # filename. Remove. Also sort. #----------------------------------- list=`echo "$list" | sed 's/..//' | sort` #-------------------------------------------- # Check each file in the list passes ChkFile #-------------------------------------------- status=0 [ -n "$list" ] && while : ; do read filename [ $? != 0 ] && break ChkFile "$filename" if [ $? != 0 ] ; then list="$filename" status=1 break; fi done <<<"$list" echo "$list" return $status } GetLst $1
- 09-16-2008 #2
So in fact, your code is almost perfect. The problem is actually one of Bash's most annoying features: it cares about whitespace.
You have this line:
The problem is that Bash requires a space before the '{':Code:ChkFile(){
This is the same reason that you need to put spaces between the 'if', '[', the condition, and the ']'.Code:ChkFile() {
It's annoying, and everyone gets caught by it initially
.
DISTRO=Arch
Registered Linux User #388732
- 09-16-2008 #3Just Joined!
- Join Date
- Sep 2008
- Posts
- 6
Hmm I added those two spaces between the function's () and the { but I am getting a very similiar error, but this time with the spaces included.
Code:Retrieving listing of dir: testdir : command not found '/dircmp: line 3: syntax error near unexpected token `{ '/dircmp: line 3: `ChkFile() {
- 09-16-2008 #4
I don't think you need the brackets in your function name. Try:
Code:ChkFile {Linux User #453176
- 09-17-2008 #5
In fact, your script works for me.
Are you sure that you're running as Bash? Add a shebang line ("#!/bin/bash") to the top of the file. If you are running as Bash, what version? It works on 3.2.17.DISTRO=Arch
Registered Linux User #388732
- 09-18-2008 #6Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
The code works on my ubuntu 8.04 with bash 3.2.39 and opensuse 10.3 with bash 3.2.25, with or without the #!/bin/bash.


Reply With Quote