2

I'm writing a bash script which takes files and directories as arguments.

It will loop through its arguments and rename the current argument if some condition is met. When this happens I want also to repeat the current loop iteration.

Can I do such thing without resorting to while or c-style for loops?

2 Answers 2

4

Use while instead of if when checking the "some condition".

for file in "$@" ; do
    while [[ $file ... ]] ; do
        mv "$file" ...
    done
done
2
  • Without while or c-style seems bit odd. Commented May 12, 2016 at 16:07
  • @Keys: Bash doesn't have a redo... Commented May 12, 2016 at 16:08
0
for FILE in <Your_Path>
do
if [ -f $args ]; then
    if [ <check_your_condition_here> ]; then
    mv $args $args_renamed
    fi
elif [ -d $args ]; then
    if [ <check_your_condition_here> ]; then
    mv $args $args_renamed
    fi
fi
done

Simply set the flags to repeat the loop for that iteration, which needs to be dynamic considering the runtime values. IHTH

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.