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
4

1 回答 1

1

forshell 脚本中的 ( ) 循环需要 ado作为其头部的一部分。您还应该引用您的变量,尤其是在处理文件名时,否则您的脚本会在文件名上出现空格。

例如:

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

您还应该考虑cd使用绝对路径并缩进代码以获得更好的可读性,而不是进入目录。

于 2013-08-01T12:57:27.293 回答