2

I am new to shell scripting and I am trying to write a simple script to run a number of files through a program. I am trying to take a file, all of which begin with the name restart and are located in the r0970.t240 directory, and copy an individual file to the file st2.res which is the name of the file the program I am running takes in and analyzes and is located in a directory called crystal. This is the process I wish to repeat for all the files. I have made an attempt at doing so, but when I attempt to run the script I get the following error message:

 line 3: syntax error near unexpected token `cp'.

I know there's a lot of specificity when it comes to shell scripts in terms of spaces and symbols and what not, so I am most likely overlooking something but due to my inexperience I don't know quite what. Any help would be much appreciated.

Here is the script in question:

cd ~Documents/work/useful/r0970.t240
for file in restart*
cp $file ~/desktop/crystal/st2.res
cd ~/desktop/crystal
./a.out
rm st2.res
done

1 Answer 1

1

A (for) loop in shell scripting requires a do as part of its head. You should also quote your variables, especially when dealing with filenames, otherwise your scripts will break on filenames with spaces.

E.g.:

for file in restart*; do
#                   ^^^^
    cp "$file" ~/desktop/crystal/st2.res
    [...]
done

You should also consider, rather than cding into a directory, to use absolute paths and to indent your code for better readability.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, this fixed my problem perfectly. I will keep these tips in mind as I continue looking into shell scripting.
Thanks for your help! Still getting to know the site :-P

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.