0

I have a bash script for reading data and creating images of it. I use a bash script for each file number(100-150) in a for loop to create the images for each of them.

#!/bin/bash

echo "PATH = $PATH";
echo "SHELL = $SHELL";

directory="../run-folders/Gritschneder_et_al_2009/3D_stirMin\=2_stirMax\=20_solWeight\=0.5/Ionization\ Data/dbl_files"        

Resolution=100

for i in `seq 100 150`;
    do

        echo ./surf $directory $i $Resolution $Resolution $Resolution   
    done    


for j in `seq 100 150`;
    do
            python plotsurfdens.py $directory $j $Resolution 
    done    

rm -rf $directory/*.txt

When I run the script it produces the following message from the executable "surf" where I have checked if the number of arguments is as expected by :-

if(argc!=6)   //TO check if no of arguments correct
    cout<<"Usage : ./read filedirectory fileno N[X] N[Y] N[Z]"<<endl;

This should not be happening as the number of arguments passed is 6 in the script. Also when I echo the command of running "surf" and copy the printed command,it works. Which means that when I manually type the command I give as a line in a script, it works but not in the script. Why is this happening? Thanks in advance :)

3
  • Print argv[i] in your surf to find out! Commented Jun 11, 2018 at 15:47
  • Put quotes around $directory. Commented Jun 11, 2018 at 16:25
  • 2
    Always quote your variables unless you have a good reason not to. Commented Jun 11, 2018 at 16:26

1 Answer 1

1

Escapes are not processed when expanding a variable value. Take the backslashes out of the directory assignment, and quote the variable when you use it.

#!/bin/bash

echo "PATH = $PATH";
echo "SHELL = $SHELL";

directory="../run-folders/Gritschneder_et_al_2009/3D_stirMin=2_stirMax=20_solWeight=0.5/Ionization Data/dbl_files"        

Resolution=100

for i in `seq 100 150`;
    do

        echo ./surf "$directory" $i $Resolution $Resolution $Resolution   
    done    


for j in `seq 100 150`;
    do
            python plotsurfdens.py "$directory" $j $Resolution 
    done    

rm -rf "$directory"/*.txt
Sign up to request clarification or add additional context in comments.

2 Comments

Yes this solved the problem, thanks a lot!! Also is it not necessary to quote the integer variable I pass $Resolution ? Or is quoting only necessary for strings that I pass. ( I realise bash does not have data types but I'm just indicating the general type that the data has by saying string or integer).
I generally recommend quoting all variables to be safe, but if you know that a variable can never contain special characters it OK to use it unquoted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.