简体   繁体   English

bash shell脚本101-在脚本中循环

[英]bash shell script 101 - loop in a script

I am trying to create a script that automates one line of command which is used for archiving selected files to tape and creating a text file with the archived files. 我正在尝试创建一个脚本,该脚本可以自动执行一行命令,该命令用于将所选文件存档到磁带上,并使用存档文件创建文本文件。 a and b are inputs and in the example below and I define them as 03 and 15. a和b是输入,在下面的示例中,我将它们定义为03和15。

a=03 a = 03

b=15 b = 15

tar -cvf /dev/tapedrive file_03 file_04 file_05 .........file_15 > /text_files/backup_file_03-15.txt tar -cvf / dev / tapedrive file_03 file_04 file_05 ......... file_15> /text_files/backup_file_03-15.txt

the script I came up with is below, 我想出的脚本如下

#! /bin/bash

a=03
b=15

for (( c=$a; c<=$b; c++ ))
$tt=" ";
do
    if[ ! $c $a ]
    then
        $c="0$c"
    fi
    $tt .= " file_".$c.""
end    

echo tar -cvf /dev/tapedrive $tt > /text_files/backup_file_$a-$b.txt

done

it's 'echo' for now instead of 'do' to make sure I get the right final command line. 现在是'echo'而不是'do'以确保我得到正确的最终命令行。 I am receiving the error 我收到错误

jag5v1.sh: line 9: syntax error near unexpected token `$tt=" "'
jag5v1.sh: line 9: `$tt=" ";'

I would appreciate any input. 我将不胜感激。 Thanks! 谢谢!

It seems that all you need to say is: 您似乎只需要说:

a=03
b=15
echo tar -cvf /dev/tapedrive $(seq -f 'file_%02g' $a $b) > /text_files/backup_file_$a-$b.txt

I have corrected your code. 我已更正您的代码。 Try this - 尝试这个 -

#! /bin/bash
a=3
b=15
tt=' ';

for c in $(seq $a $b);
do
    if [ $c -le 9 ]
    then
    c="0"$c
    fi
    tt=$tt" file_"$c
done
echo tar -cvf /dev/tapedrive $tt > /text_files/backup_file_0$a-$b.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM