在Bash中按顺序移动文件

在Bash中按顺序移动文件,bash,Bash,我正在尝试将文件夹从1移动到05,并为其分配编号 例如: test-01 => test-221 test-02 => test-222 test-03 => test-223 test-04 => test-224 test-05 => test-225 我试过这个 for num in $(seq -w $2 $3); { mv -v "test-$num" "test-$1$num" } 像这样使用它 ./script.sh 2 21 25 1 =&

我正在尝试将文件夹从1移动到05,并为其分配编号

例如:

test-01 => test-221
test-02 => test-222
test-03 => test-223
test-04 => test-224
test-05 => test-225
我试过这个

for num in $(seq -w $2 $3); {
  mv -v "test-$num" "test-$1$num"
}
像这样使用它

./script.sh 2 21 25
1 => 21
1 => 22
1 => 23
1 => 24
1 => 25
2 => 21
2 => 22
2 => 23
2 => 24
2 => 25
3 => 21
3 => 22
3 => 23
3 => 24
3 => 25
4 => 21
4 => 22
4 => 23
4 => 24
4 => 25
5 => 21
5 => 22
5 => 23
5 => 24
5 => 25
但是我得到了输出

test-21 => test-221
test-22 => test-222
test-23 => test-223
test-24 => test-224
test-25 => test-225
这当然是错误的,参见示例:-)

我也试过这样做

for a in {1..5}; {
  for b in {21..25}; {
    echo "$a => $b"
  } #b
} #a
但我得到了这样的重复输出

./script.sh 2 21 25
1 => 21
1 => 22
1 => 23
1 => 24
1 => 25
2 => 21
2 => 22
2 => 23
2 => 24
2 => 25
3 => 21
3 => 22
3 => 23
3 => 24
3 => 25
4 => 21
4 => 22
4 => 23
4 => 24
4 => 25
5 => 21
5 => 22
5 => 23
5 => 24
5 => 25

你只需要一个循环。但无论你用哪种方式计算,你在一个目的中使用的数字在另一个目的中都是错误的。因此,您需要使用算术来获得正确的数字,例如:

$(( $num + 220 ))

你只需要一个循环。但无论你用哪种方式计算,你在一个目的中使用的数字在另一个目的中都是错误的。因此,您需要使用算术来获得正确的数字,例如:

$(( $num + 220 ))

这是我到目前为止所拥有的

for i in $(seq -w $1 $2); {
  echo "test-$i => test-$((i+$3))"
} #for
通过运行获取的输出。/script.sh 01 05 220

test-01 => test-221
test-02 => test-222
test-03 => test-223
test-04 => test-224
test-05 => test-225

因此,它看起来与我的示例相匹配:-)

这是我到目前为止得到的结果

for i in $(seq -w $1 $2); {
  echo "test-$i => test-$((i+$3))"
} #for
通过运行获取的输出。/script.sh 01 05 220

test-01 => test-221
test-02 => test-222
test-03 => test-223
test-04 => test-224
test-05 => test-225

因此,它看起来与我的示例相匹配:-)

多亏了你们的帮助,这就是我现在所拥有的

for i in $(seq -w $1 $2); {
  echo "test-$i => test-$((10#$i+$3))"
} #for
这是我运行时得到的输出。/script.sh 01 10 220例如

test-01 => test-221
test-02 => test-222
test-03 => test-223
test-04 => test-224
test-05 => test-225
test-06 => test-226
test-07 => test-227
test-08 => test-228
test-09 => test-229
test-10 => test-230
这正是我想要实现的。非常感谢“罗宾·格林”和“格尼奥夫·格尼奥夫”

现在,目录的移动工作正常^_^

mv -v "temp/test-$i" "temp/test-$((10#$i+$3))"
移动目录的输出为

‘temp/test-01’ -> ‘temp/test-221’
‘temp/test-02’ -> ‘temp/test-222’
‘temp/test-03’ -> ‘temp/test-223’
‘temp/test-04’ -> ‘temp/test-224’
‘temp/test-05’ -> ‘temp/test-225’
‘temp/test-06’ -> ‘temp/test-226’
‘temp/test-07’ -> ‘temp/test-227’
‘temp/test-08’ -> ‘temp/test-228’
‘temp/test-09’ -> ‘temp/test-229’
‘temp/test-10’ -> ‘temp/test-230’
问题仅在我运行时出现/script.sh 01 10 0这是我得到的输出

test-01 => test-1
test-02 => test-2
test-03 => test-3
test-04 => test-4
test-05 => test-5
test-06 => test-6
test-07 => test-7
test-08 => test-8
test-09 => test-9
test-10 => test-10

正如你所看到的,没有前导零=(

多亏了大家的帮助,这就是我现在拥有的

for i in $(seq -w $1 $2); {
  echo "test-$i => test-$((10#$i+$3))"
} #for
这是我运行时得到的输出。/script.sh 01 10 220例如

test-01 => test-221
test-02 => test-222
test-03 => test-223
test-04 => test-224
test-05 => test-225
test-06 => test-226
test-07 => test-227
test-08 => test-228
test-09 => test-229
test-10 => test-230
这正是我想要实现的。非常感谢“罗宾·格林”和“格尼奥夫·格尼奥夫”

现在,目录的移动工作正常^_^

mv -v "temp/test-$i" "temp/test-$((10#$i+$3))"
移动目录的输出为

‘temp/test-01’ -> ‘temp/test-221’
‘temp/test-02’ -> ‘temp/test-222’
‘temp/test-03’ -> ‘temp/test-223’
‘temp/test-04’ -> ‘temp/test-224’
‘temp/test-05’ -> ‘temp/test-225’
‘temp/test-06’ -> ‘temp/test-226’
‘temp/test-07’ -> ‘temp/test-227’
‘temp/test-08’ -> ‘temp/test-228’
‘temp/test-09’ -> ‘temp/test-229’
‘temp/test-10’ -> ‘temp/test-230’
问题仅在我运行/script.sh 01 10 0时出现;这是我得到的输出

test-01 => test-1
test-02 => test-2
test-03 => test-3
test-04 => test-4
test-05 => test-5
test-06 => test-6
test-07 => test-7
test-08 => test-8
test-09 => test-9
test-10 => test-10

因为您看不到前导零=(

尝试使用
awk

#!/bin/bash

awk -v fa=$1 -v fb=$2 -v ta=$3  'BEGIN {  for(i=fa;i<=fb;i++) printf "test-%02d => test-%02d\n",i,i+ta  }'

根据OP的要求,漂亮的多行代码

#!/bin/bash

awk -v fa=$1 -v fb=$2 -v ta=$3  'BEGIN {  
  for(i=fa;i<=fb;i++) 
    printf "test-%02d => test-%02d\n",i,i+ta
}'
!/bin/bash
awk-v fa=$1-v fb=$2-v ta=$3'开始{

对于(i=fa;i尝试使用
awk

#!/bin/bash

awk -v fa=$1 -v fb=$2 -v ta=$3  'BEGIN {  for(i=fa;i<=fb;i++) printf "test-%02d => test-%02d\n",i,i+ta  }'

根据OP的要求,漂亮的多行代码

#!/bin/bash

awk -v fa=$1 -v fb=$2 -v ta=$3  'BEGIN {  
  for(i=fa;i<=fb;i++) 
    printf "test-%02d => test-%02d\n",i,i+ta
}'
!/bin/bash
awk-v fa=$1-v fb=$2-v ta=$3'开始{

因为(i=fa;i从我们在评论中进行的所有有趣的讨论(这是一条非常有趣的线索),我或多或少(可能更少)理解你想要什么……或者更确切地说,我对我认为你想要什么有自己的想法。让我重新表述我理解的内容(如果这不是你确切要求的,请原谅我)

您需要一个脚本,该脚本将包含三个非负数值参数
X
Y
Z
(可能带前导
0
),其中:

  • M
    的范围从
    X
    Y
    ,左侧填充
    0
    ,以便
    M
    中的字符数是
    X
    Y
    中的最大字符数
  • N=M+Z
    ,左填充
    0
    ,使
    N
    中的字符数是
    X
    Y
    Z
    Y+Z
    的最大字符数。例如

    $ ./script 01 04 00220
    test-01 => test-00221
    test-02 => test-00222
    test-03 => test-00223
    test-04 => test-00224
    
    $ ./script 99 101 0
    test-099 => test-099
    test-100 => test-100
    test-101 => test-101
    
    $ ./script 99 101 00000
    test-099 => test-00099
    test-100 => test-00100
    test-101 => test-00101
    
    $ ./script 00 02 99
    test-00 => test-099
    test-01 => test-100
    test-02 => test-101
    
此外,您还需要一个bash解决方案,这样您就可以
mv
相应的文件,而无需解析另一个命令的输出

现在我们开始,希望您也能找到一些有趣的东西来挖掘(注意,输出的形式是
mv-nv-xxx-yyy
,而不是
test-x=>test-y
;如果您对此满意,请删除
echo
):

!/bin/bash
预结束源=测试-
预结束目标=测试-
附加源=
附加目标=
shopt-s extglob
die(){printf>&2“%s\n”“$@”退出1;}
是_number(){[$1=+([[:digit:]]]];}
是_在_范围内(){[[-z${1//0/}]][${1/#+(0)}=$((10#$1))]}
maxlength(){
本地u l=0 retvar=1美元
转移
因为我在“$@”中是这样做的
u=${i}
((u>l))&((l=u))
完成
printf-v“$retvar”“%d”“$l”
}
X=1美元
Y=2美元
Z=3美元
is_number“$X”| | die“第一个参数不是有效数字”
is_number“$Y”| | die“第二个参数不是有效数字”
is_number“$Z”| | die“第三个参数不是有效数字”
((10#$X=0))| | die“第二个和最后一个参数的总和超出范围”
最大长度“长度”\u“$X”$Y
maxlength“length_t”“$X”“$Y”“$Z”“$((10#$Y+10#$Z))”

因为((i=10#$X;i从我们在评论中进行的所有有趣的讨论(这是一条非常有趣的线索),我或多或少(可能更少)理解你想要什么……或者更确切地说,我对我相信你想要的东西有了自己的想法。让我重新表述我理解的内容(如果这不是你确切要求的,请原谅我)

您需要一个脚本,该脚本将包含三个非负数值参数
X
Y
Z
(可能带前导
0
),其中:

  • M
    的范围从
    X
    Y
    ,左侧填充
    0
    ,以便
    M
    中的字符数是
    X
    Y
    中的最大字符数
  • N=M+Z
    ,左填充
    0
    ,使
    N
    中的字符数是
    X
    Y
    Z
    Y+Z
    的最大字符数。例如

    $ ./script 01 04 00220
    test-01 => test-00221
    test-02 => test-00222
    test-03 => test-00223
    test-04 => test-00224
    
    $ ./script 99 101 0
    test-099 => test-099
    test-100 => test-100
    test-101 => test-101
    
    $ ./script 99 101 00000
    test-099 => test-00099
    test-100 => test-00100
    test-101 => test-00101
    
    $ ./script 00 02 99
    test-00 => test-099
    test-01 => test-100
    test-02 => test-101
    
此外,您还需要一个bash解决方案,这样您就可以
mv
相应的文件,而无需解析另一个命令的输出

现在我们开始,希望您也能找到一些有趣的东西来挖掘(注意,输出的形式是
mv-nv-xxx-yyy
,而不是
test-x=>test-y
;如果您对此满意,请删除
echo
):

!/bin/bash
预结束源=测试-
预结束目标=测试-
附加源=
附加目标=
shopt-s extglob
die(){printf>&2“%s\n”“$@”退出1;