简体   繁体   English

for 循环只执行一次 | bash shell

[英]for loop only get executed once | bash shell

I want to code a simple renaming script.我想编写一个简单的重命名脚本。 But Im running into the issue that the loop only gets executed onces.但是我遇到了循环只执行一次的问题。 The $folder consist of 6 lines with values. $folder由 6 行带值组成。

echo "Start Episode?"
read start_ep
echo "Start Season?"
read start_se
echo "$folder"
for entry in "$folder"
do
    rename_array+="S${start_se}E${start_ep}"
    let "start_ep++"
    echo "$start_ep"
done
echo "Hello"
echo ${rename_array[*]}

Expected output:预期 output:

Start Episode?
1
Start Season?
1
/home/georodin/01.txt
/home/georodin/02.txt
/home/georodin/03.txt
/home/georodin/04.txt
/home/georodin/05.txt
/home/georodin/06.txt
2
3
4
5
6
Hello
S1E1
S1E2
S1E3
S1E4
S1E5
S1E6

Output: Output:

Start Episode?
1
Start Season?
1
/home/georodin/01.txt
/home/georodin/02.txt
/home/georodin/03.txt
/home/georodin/04.txt
/home/georodin/05.txt
/home/georodin/06.txt
2
Hello
S1E1

If $folder contains new lines, try the loop like that:如果$folder包含新行,请尝试这样的循环:

while IFS= read -r entry; do
    echo "... $entry ..."
done <<< "$folder"

Edit : See also Bash: Iterating over lines in a variable编辑:另见Bash:迭代变量中的行

I think the double quote make $folder like one item in the for loop.我认为双引号使 $folder 像 for 循环中的一个项目。 Delete them !删除它们!

for entry in $folder
do
    rename_array+="S${start_se}E${start_ep}"
    let "start_ep++"
    echo "$start_ep"
done

This should work with your initial idea.这应该适用于您最初的想法。 Be careful with quotes in bash they are very powerful:)小心 bash 中的引号,它们非常强大:)

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

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