Question: Shell Script Create a shell script, cExt.sh that takes one or more parameters, where the first is a desired extension and the remainder are names

Shell Script

Create a shell script, cExt.sh that takes one or more parameters, where the first is a desired extension and the remainder are names of files to be renamed. For each file in the command line, this script should rename the file, if the file exists or print the error message file does not exist.

For example,

ls > alligator.foo

echo meow > dango.bar

./cExt.sh dat alligator.foo madeup.foo dango.bar

should result in alligator.foo being renamed alligator.dat, an error message madeup.foo: No such file, and dango.bar being renamed dango.dat.

this is what I have:

#!/bin/sh

ext=$1

oldName=$2

while [ "$oldName" != "" ]; do ###This is where I'm confused....still learning loops. It runs continuously.

if [ ! -f "$oldName" ]; then

echo "$2: No such file"

else

sedTime=`echo $oldName | sed 's/\.[^.]*$//'`

newName=$sedTime.$ext

if [ "$oldName" = "$newName" ]; then

exit

fi

mv "$oldName" "$newName"

fi

done

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!