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

***This is in Unix*** 1) Write a Bourne Shell script called \"myMv.sh\" (which s

ID: 3740050 • Letter: #

Question

***This is in Unix***

1) Write a Bourne Shell script called "myMv.sh" (which simulates the GNU utility mv) that tries to rename the specified file (using the GNU utility mv), but if the destination file exists, instead creates an index number to append to the destination file, a sort of version number. So if I type: ?myMv.sh a.txt b.txt but b.txt already exists, myMv.sh will move the file to b.txt.1. Note that if b.txt.1 already exists, you must rename the file to b.txt.2, and so on, until you can successfully rename the file to a name that does not already exist. 2) Write a Bourne Shell script named as "convert.sh" to accept a positive decimal number and an option from command line. If the option is -b, the output is the binary representation of that number. If the option is -o, the output is the octal representation of that number. Sample output s./convert.sh -b 11 The binary representation of 11 is 1011 s./convert.sh The octal representation of 11 is 13

Explanation / Answer


#!/bin/bash
# Script to rename the ifile if same given name exist then rename it.
echo "Welcome to Bash script for renamning files."
if [[ $# != 2 ]]; then
#error
echo "You have not entered valid files eneter source and destination file after command."
fi
source1=$1
destination=$2
var=1
# loop to check if filename exixsts or nor
while [[ -f "$destination" ]]; do
#if exixsts rename it to file.1 if that also exists ahcnge it to file.2
echo "same name found"
# extending the name if file exists
destination="$destination.$var"
var=var+1
done
# renaming file using mv command with no output
mv "$source1" "$destination" #2>/dev/null
if [ $? -eq 0 ]; then
echo "Filensme $source sucessfully changed to $destination "
else
echo "Error File not changed."
fi

#end of code

OUTPUT OF THE CODE:

dps@machine:~/Desktop/New$ chmod 777 myMv.sh
dps@machine:~/Desktop/New$ ls
ds1.txt ds1.txt.1 myMv.sh run.sh test.sh
dps@machine:~/Desktop/New$ ./myMv.sh ds1.txt ds1.txt.1
Welcome to Bash script for renamning files.
same name found
Filensme sucessfully changed to ds1.txt.1.1


#!/bin/bash
# Script to convert decimal to hexadecimal and binary
echo "Welcome to Bash script for renamning files."
if [[ $# != 2 ]]; then
#error
echo "You have not entered valid files eneter source and destination file after command."
fi
# assigning values to varibles
to=$1
num=$2
if [[ $to = "-b" ]]; then
#if input is to convert to binary
echo "The binary representaion of $num is:"
echo 'obase=2;'$num | bc
elif [[ $to = "-o" ]]; then
#convert to octal
echo "The Octal representaion of $num is:"
echo 'obase=8;'$num | bc
else
echo "You have not enetered a valid choice, example execution is convert.sh -o 11"
fi
#end of script

OUTPUT:

dps$ chmod 777 convert.sh
dps$ ./convert.sh -o 11
Welcome to Bash script for renamning files.
TO and num is: -o 11
The Octal representaion of 11 is:
13
dps@machine:~/Documents/Chegg$ ./convert.sh -b 11
Welcome to Bash script for renamning files.
The binary representaion of 11 is:
1011