1

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

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

for file in "$@" ; do
    while [[ $file ... ]] ; do
        mv "$file" ...
    done
done
  • Without while or c-style seems bit odd. – Keyshov Borate May 12 '16 at 16:07
  • @Keys: Bash doesn't have a redo... – choroba May 12 '16 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.