进入for循环后获取变量的原始值

问题描述 投票:0回答:1

我正在制作一个简单的脚本,该脚本遍历当前目录中的文件,$1用于大小参数,其他$1,$2 .....用于操作文件并设置文件名。问题是使用for循环后,变量将丢失其值,并以1,2,3之类的整数开头,除非我使用名为1,2,3,...的文件,否则脚本将无法工作。如何保留原始值?例如:./script 50 my_first_file .....

#!/bin/bash
size=$1
allfiles=$#
shift
#here the value of the $1 is "my_first_file"
  for ((i = 1 ; i < allfiles ; i++))
  do
  #here the value of the $1 = 1
  done
bash for-loop
1个回答
0
投票

而不是使用带有整数的for循环,您可以像这样直接在参数上循环:

#!/bin/bash

size=$1
allfiles=$#
shift

for i in $@
do
    echo $i
done

这将按顺序显示每个参数。

© www.soinside.com 2019 - 2024. All rights reserved.