Bash syntax error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash syntax error
# 1  
Old 09-16-2014
Bash syntax error

Code:
while read line
do
    mkdir $line
    scp -r Docking_results/docking_$line.pdb $line/
    cd /$line/
    set a=`grep ENDMDL docking_'$line'.pdb | wc -l`
    set b=`expr $a - 2`
    csplit -k -s -n 3 -f docking_'$line'. docking'$line'.pdb '/^ENDMDL/+1' '{'$b'}'
    foreach f ( docking'$line'.[0-9][0-9][0-9] )
      mv $f $f.pdb
    end
    cd ..
done<file

When I run the above script I get an error in the foreach line:
Code:
./docking_cluster.sh: line 9: syntax error near unexpected token `('
./docking_cluster.sh: line 9: `    foreach f ( docking'$line'.[0-9][0-9][0-9] )'


I'm not sure how to pass the $line variable correctly.

File is a file containing a list of strings
$line is a particular string in file
# 2  
Old 09-16-2014
The 'foreach f' looks like you're trying to translate an MSDOS 'foreach /f' loop.

Code:
for f in docking"$line".[0-9][0-9][0-9]
do
        echo "$f"
done

# 3  
Old 09-16-2014
Code:
  set a=`grep ENDMDL my_docking.pdb | wc -l`
    set b=`expr $a - 2`
    csplit -k -s -n 3 -f my_docking. mydocking.pdb '/^ENDMDL/+1' '{'$b'}'
    foreach f ( mydocking.[0-9][0-9][0-9] )
      mv $f $f.pdb
    end

This is a code I know works.

The only thing I changed was to add a loop over the strings in file file.

So I think the problem is the way $line is passed through.

What's the difference between

Code:
$line, "$line" and '$line'

?
# 4  
Old 09-16-2014
Quote:
Originally Posted by chrisjorg
Code:
  set a=`grep ENDMDL my_docking.pdb | wc -l`
    set b=`expr $a - 2`
    csplit -k -s -n 3 -f my_docking. mydocking.pdb '/^ENDMDL/+1' '{'$b'}'
    foreach f ( mydocking.[0-9][0-9][0-9] )
      mv $f $f.pdb
    end

This is a code I know works.

The only thing I changed was to add a loop over the strings in file file.

So I think the problem is the way $line is passed through.

What's the difference between

Code:
$line, "$line" and '$line'

?
Sorry, but we don't believe you. The foreach is not valid bash syntax. Are you sure you didn't run the above code which a csh derivative instead of with bash?

And, yes, you also need to change single quotes in '$line' to double quotes "$line" throughout your script (as Corona688 suggested in his replacement for your foreach loop).
# 5  
Old 09-16-2014
The first thing I've noticed is the missing shebang/hashbang (something like #!/bin/bash) in the very first line.

cd /$line/ I think the first slash should be removed, unless you're running this script from the root "/" directory.

set a=`grep ENDMDL my_docking.pdb | wc -l` should not work in bash. Try a=`grep ENDMDL my_docking.pdb | wc -l` instead.

Demonstration:
Code:
$ set a=`uname`
$ echo $a

$ a=`uname`
$ echo $a
Linux
$

Quote:
Originally Posted by chrisjorg
What's the difference ...
$line: variable will expand
"$line": variable will expand safely, e.g. preserving whitespaces in filenames (good practice + recommended)
'$line': variable will *not* expand, it's treated as a *literal* string $line

Demonstration:
Code:
$ test -d "VirtualBox VMs"
$ echo $?
0
$ 
$ for name in "VirtualBox VMs"; do ls $name; done
ls: cannot access VirtualBox: No such file or directory
ls: cannot access VMs: No such file or directory
$ 
$ for name in "VirtualBox VMs"; do ls "$name"; done
testvm1
$ 
$ for name in "VirtualBox VMs"; do ls '$name'; done
ls: cannot access $name: No such file or directory
$

Most likely, '{'$b'}' also needs to be replaced by either ${b} or "$b"

HTH
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Syntax error in subtraction in Bash

I am sharing a code snippet. for (( i=0; i<=$(( $count -1 )); i++ )) do first=${barr2} search=${barr1} echo $first echo "loop begins" for (( j=0; j<=5000; j++ )) do if } == $search ]]; then echo $j break; fi done second=${harr2} echo $second (2 Replies)
Discussion started by: ngabrani
2 Replies

2. BSD

Keep getting error "-bash: ./.profile_z2: line 52: syntax error: unexpected end of file"

#!/bin/bash #-------------------------------------------------------- # Setup prompt # Author Zeeshan Mirza # Data: 06-08-2017 #-------------------------------------------------------- if then . ./.profile_custom_pre fi umask 022 set -o vi export EDITOR=vi export VISUAL=vi... (3 Replies)
Discussion started by: getzeeshan
3 Replies

3. Shell Programming and Scripting

Bash calling a few functions syntax error

In the bash function below if the user selets "y" then the menu function is called and if they select "n" the move function is called. That all seems to work, my question is after the files are moved an echo, line in bold is displayed and another function called backup is called. I am getting a... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

Bash function using variable in it syntax error

The below bash function uses multiple variables CODING, SAMPLE, SURVEY, andvariant in it. The user selects the cap function and details are displayed on the screen using the $SURVEY variable, the directory is changed to $SAMPLE and the samples.txt is opened so the user can select the sample to... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. Shell Programming and Scripting

-bash: syntax error near unexpected token `('

// AIX 6.1 I am getting a syntax error below. Please advise what to be corrected. :confused: runmqsc CERN.$(echo `hostname` | cut -d'.' -f1 | tr '' '').$(echo $environment | tr '' '') <<! | egrep -i '(FROM.NC.APPLIANCE)' | sort -u |awk '{print $2}' | cut -d '(' -f2 | cut -d ')' -f1 |... (1 Reply)
Discussion started by: Daniel Gate
1 Replies

6. Shell Programming and Scripting

Trying to pass a password: bash: syntax error near unexpected token `('

howdy, so I'm make a plugin work for Nagios, and the commandline is: /usr/lib/nagios/plugins/check_mssql -H MySQLServerName -u MySqlAccountName -p MyPassword(#XXXXX -d MyDatabaseName it is barfing with: bash: syntax error near unexpected token `(' Thoughts? Do I have to wrap something... (2 Replies)
Discussion started by: rgouette
2 Replies

7. Shell Programming and Scripting

Bash syntax

Hello, I have seen this syntax, { ;;};quite often and I don't know what it means exactly. It seems like a distinctive thing of Bash, so it's been used for the logo of the last bug, ShellShock: All you need to know about the Bash Bug vulnerability | Symantec Connect I have also seen... (3 Replies)
Discussion started by: Kibou
3 Replies

8. Shell Programming and Scripting

Bash (Ubuntu server): Syntax error: "|" unexpected in While-loop

Hello forum, I hope my problem is easy to solve for someone in here! My main task is to copy a large amount of imap-accounts from one server to another. There is a tool (Perl) called imapsync which does the job exellent. Unfortunately I'm only able to run it on one account at a time. After... (3 Replies)
Discussion started by: primaxx
3 Replies

9. Shell Programming and Scripting

bash syntax error: command not found

I am trying to create a shell that asks the user to enter their name, and compare it to my own by saying we have the same name or saying my name and that they have a nice name too. Here is my script... #!/bin/bash-x echo "Enter your name". read name if then echo "My name is Adam too"... (1 Reply)
Discussion started by: amaxey45
1 Replies

10. Shell Programming and Scripting

BASH Script syntax error

I'm trying to write a simple script that takes all the .tar.gz files in a directory and verifies them by using the gzip -tv command: for zip in *.tar.gz do gzip -tv $zip if ; then #Check return code from tar echo "File ${zip} verified OK." exit... (4 Replies)
Discussion started by: kelldan
4 Replies
Login or Register to Ask a Question