Question

In: Computer Science

Write a Bash script called move that could replace the UNIX command mv. 'move' tries to...

Write a Bash script called move that could replace the UNIX command mv. 'move' tries to rename the source file (using the UNIX command mv), but if the destination file exists, appends an index number, a sort of version number, to the destination file. So if the user types:

move a.txt b.txt

and b.txt already exists, move will rename the file to b.txt.1. If b.txt.1 already exists, move must rename the file to be b.txt.2, and so on, until the file can be successfully renamed with a name that does not already exist.

This is what I have so far but for some reason instead of indexing the filenames .1, .2, .3 it does .1, .1.1, .1.1.1 which I am confused on how to fix.

#!/bin/bash

if [ -f "$1" ] && [ -f "$2" ]; then
x = 1

while [ -f $2"."$x ]
do
x=$((x+1))
done

mv $1 $2"."$x

else
mv $1 $2
fi

Solutions

Expert Solution

The solution to the above question is given below with screenshot of output.

=====================================================

I kept the logic simple and i have tested all outputs.

If there is anything else do let me know in comments.

=====================================================

============ CODE TO COPY ===============================

move

#!/bin/bash

if [ -f "$1" ] && [ -f "$2" ]; then

   x=1

   while [ -f $2"."$x ]
   do

       x=$((x+1))

   done

   mv $1 $2"."$x


else

   mv $1 $2

fi


=====================================================
Output :


Related Solutions

UNIX ONLY Write a bash script that will accept a filename as a command line argument....
UNIX ONLY Write a bash script that will accept a filename as a command line argument. Your script should first check whether the filename has been passed as argument or not (hint: at least one argument has to be provided). If no argument has been provided your script should display appropriate message and exit. If a file name has been provided, your script should check if the file exists in the current directory and display the contents of the file....
Write a C Unix shell script called showperm that accepts two command line parameters. The first...
Write a C Unix shell script called showperm that accepts two command line parameters. The first parameter should be an option flag, either r, w or x. The second parameter should be a file name. The script should indicate if the specified file access mode is turned on for the specified file, but it should display an error message if the file is not found. For example, if the user enters: showperm r thisfile the script should display “readable” or...
Write a BASH shell script (see the Important Notes section) that acts as a DOS command...
Write a BASH shell script (see the Important Notes section) that acts as a DOS command interpreter (the user enters the DOS command and the script executes the corresponding Linux command). The script should loop continuously until the user enters the QUIT command Prior to accepting a command, display a prompt containing your first name and the greater than symbol (>) Example prompt: linux> The DOS command, and any arguments, should be stored in variables $command – the DOS command...
Simple shell scripting Write a Bash shell script called strcount.sh that counts the number of occurances...
Simple shell scripting Write a Bash shell script called strcount.sh that counts the number of occurances of a given string within a file. The scripts takes two arguments: the name of the file to check and the string to count occurances of. If the file is not found (or not readable), the script should print the error message Cannot access file and exit with code -1. Otherwise, the script should print the number of occurances of the given string and...
Unix Create a script that will declare an array and assign four values from the command...
Unix Create a script that will declare an array and assign four values from the command line. 1. Use these four values - Paul, Ringo, George, John, - in that order 2. Display the content of the array, displaying the values in this format and this order The first array value is "John" The second array value is "Paul" The third array value is "George" The fourth array value is "Ringo"
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
In this assignment, you are required to write a Bash script, call it assignment2.sh. Your Bash...
In this assignment, you are required to write a Bash script, call it assignment2.sh. Your Bash script has to accept at least four input arguments, and must: 1) Print to the user a set of instructions explaining how the PATH variable can be used in Bash. 2) Save the manual of the 'awk' command in the file /tmp/help.txt. 3) Shut down your Ubuntu box at 2 o'clock tonight. 4) Store your name and your partner's name in a text file...
Fully Functional Script written in BASH In this section, you will write a fully functional script...
Fully Functional Script written in BASH In this section, you will write a fully functional script to help your manager with an important project that will build upon what you learned from your script work in Milestone Four. After you have written the script, you will execute it to generate a report for all three users (once for Bob, once for Henry, and once for Frank). You should have three unique generated reports in your ~/scripts directory (or more if...
write an awk script that works as wc command.
write an awk script that works as wc command.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT