简体   繁体   中英

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.

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:

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:

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:

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

Edit : See also Bash: Iterating over lines in a variable

I think the double quote make $folder like one item in the for loop. 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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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