Thursday, August 11, 2016

command line - Script to swap the names of two files

I am new to Bash scripting. I have been trying to make a script that will swap the file names of the two files passed to it by the user.



Here's a picture of the two versions of my script so far



Here is the script in text format with mv:



#! /bin/bash
file1=$file1

file2=$file2

echo "write file name :"
read file1 file2

if [[ $file1 = $file2 ]]
then
cp $file1 $file1
mv $file1 $file2
fi


if [[ $file2 = $file1 ]]
then
mv $file2 $file1
fi


But my question is if I can make a script that let the user write down 2 filenames first, then the script will swap the 2 file names



The basic of swapping file names what I have read is this




cp $file1 temporaryfile
mv $file1 $file2
mv $file2 temporyfile

No comments:

Post a Comment

11.10 - Can't boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...