Home > Software design >  How to change a filename that's input from keyboard
How to change a filename that's input from keyboard

Time:12-12

I'm trying to answer a school assignment question that wants me to take whatever filenames are input and check if the file ends in .old. If it doesn't to change the name to end with .old.

It needs to have a structure like what I have here but I'm getting an error with this and I'm not sure how to fix it.

#!/bin/bash

for filename in $(ls $1 | grep -v ".old$")
do
        mv $filename $filename.old
done

CodePudding user response:

 $ filename=file.new
 $ if [ "$filename" != "${filename%.old}.old" ]; then echo mv $filename ${filename%.old}.old; else echo $filename - nothing to do; fi
 mv file.new file.new.old
 $ filename=file.old
 $ if [ "$filename" != "${filename%.old}.old" ]; then echo mv $filename ${filename%.old}.old; else echo $filename - nothing to do; fi
 $ file.old - nothing to do

CodePudding user response:

The 0 in your loop ensures that your loop is always true, so you always have to increase your counting variable by one

  • Related