Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a shell script, cExt.sh that takes one or more parameters, where the firs

ID: 3916721 • Letter: C

Question

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

echo snake > queen cobra.cpp

./cExt.sh dat alligator.foo madeup.foo dango.bar queen cobra.cpp

should result in alligator.foo being renamed alligator.dat, an error message “madeup.foo: No such file”, dango.bar being renamed dango.dat., and queen cobra.cpp being renamed to queen cobra.dat

My only issue is the spacing in queen cobra.cpp and changing it from queen cobra.cpp to queen cobra.dat

please help

#!/bin/sh

if [ $# -le 1 ]
then
echo "Usage: $0 <ext> <file1> [file2 .... fileN]"
exit 1
fi
ext=$1

shift
oldName="$@"

for f in $oldName
do
if [ ! -f "$f" ]
then
echo "$f: No such file"
else

sedTime=`echo $f | sed 's/.[^.]*$//'`
newName=$sedTime.$ext

if [ "$f" != "$newName" ]
then

mv "$f" "$newName"
fi
fi
done

Explanation / Answer

First of all, echo snake > queen cobra.cpp will create a file named queen and not  queen cobra.cpp. Please verify that. So in order to call your script the correct way of calling will be

echo snake > "queen cobra.cpp"

then invoke your script using

./cExt.sh dat alligator.foo madeup.foo dango.bar "queen cobra.cpp"

So your script is perfectly working. Let me know through comments, the name of the file that is created using echo snake > queen cobra.cpp. So if it is queeen, your code is working correct.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote