LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   A shell Script to exchange the content of two files (https://www.linuxquestions.org/questions/linux-newbie-8/a-shell-script-to-exchange-the-content-of-two-files-703151/)

rankmar 02-08-2009 12:33 PM

A shell Script to exchange the content of two files
 
Hallo all

Could someone help me? I am absolute beginner in shell script Programming.
I was asked to write a shell script that can automatically exchange the content of two files, independent of present working directory. Meanwhile , the script should print the text „THE CONTENT OF FILE:“ before the actual content of the files on the screen. Only 4 commands must be used in the assignment:
2 copy, 1 mv and 1rm commands.


Currently the files which content should be exchanged are located in:
FILE1: ~/subdir1/file1
FILE2: ~/subdir1subdir2/file2

Thanks in advance

ranks

makuyl 02-08-2009 12:48 PM

You could start by reading at least parts of these:
http://tldp.org/LDP/Bash-Beginners-G...ers-Guide.html
http://www.freeos.com/guides/lsst/

Other than that, post what you have tried to start with, and someone will help when you get stuck, but you'll have to at least try.

H_TeXMeX_H 02-08-2009 01:24 PM

Oh, come on, it's far too simple for us to give you the answer, we shouldn't anyway because it will not help you learn anything. Think about what you would do in any language, how would you spend those cp, mv, and rm commands ? Take a look at the man pages 'man cp', 'man rm', 'man mv'.

i92guboj 02-08-2009 03:03 PM

Do it in human language, once you have the steps described in a readable manner, take your class notes and read them.

We are not doing the homework for you. We can help you with concrete problems if you get stuck, though.

rankmar 02-08-2009 05:51 PM

A shell Script to exchange the content of two files
 
Thanks for your effort. It is not my way of solving assignment problems. Unfortunately I only have two and the last pages in my book that handle shell script programming, then I will go on another subject. That means know proper orientation point for task of this kind. I will see what I can do with the available information already on this forum.

kauuttt 02-09-2009 03:39 AM

Try to think in very simple way...suppose you have 2 bags of books :)what you will do to exchange them....think this first..read man page of cp, rm and mv...and try with a simple script...if it doesnot work, post that...we will help you (it will be hardly 2-3 lines)...and if u succeed u will gain lot of confidence...

happy coding..

cheers-
kd

makuyl 02-09-2009 10:18 AM

I would say start thinking post-it notes. You have two notes on a table, one on the left says "hello", one on the right says "bye".
At the end a note can cover another beneath it.
2 copy, 1 move (and 1 remove) to change the left one to say bye, and right one to say hello.

As a side note, type "alias" in your shell. If you see:
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
You need to unalias or figure out how to force the commands in the script. "-i" asks before overwriting.

rankmar 02-09-2009 03:36 PM

You are absolutely right that I should first make effort myself. I don't want this to be a barrier in my programming carrier. I must try my best. If no success I will post the script here.

regards

kauuttt 02-10-2009 01:07 AM

Sure....it is great to see ur spirits.
Good luck !!:)

rankmar 02-12-2009 09:04 AM

write the shell script that can automatically exchange the content of two files
 
Hallo all,


I still can't figure out how to correctly write the shell script that can automatically exchange the content of two files, independent of present working directory. This is what I have so far done.

THE SHELL SCRIPT:

Code:

clear

echo

cp  ~/linutest/subdir/da2 ~/esa4/ $1

echo -e "\n\t The content of  da1: "

echo

cp  ~/esa4/da1 ~/linutest/subdir/  $2

echo -e "\n\t The content of da2: "

echo

mv ~/esa4/da2  ~/linutest/subdir/da1

rm ~/linutest/subdir/da1

echo


Now the script can automatically tranfer the content of the file „da2“ to file „da1“, but can't tranfer the content of file „da1“ to „da2“ . The script should set back the content of the files when run the second time but it is not the case.

Any idea of what to do to make it work properly?

Thanks in advance

ranks

H_TeXMeX_H 02-12-2009 10:05 AM

It's a bit tricky because normally you would use 1 mv instead of the cp + rm combo. So here's my hint:
The order should be cp, mv, cp, rm.
Example:

Code:

bash-3.1$ find
.
./file1
./new
./new/file2
bash-3.1$ cp file1 new/file-temp
bash-3.1$ mv new/file2 file1
bash-3.1$ cp new/file-temp new/file2
bash-3.1$ rm -f new/file-temp

Run the sequence again and it will switch them.

rankmar 02-12-2009 12:11 PM

Thanks H_TeXMeX_H, please what do you mean by file-temp and new? Are they folders?


regards

ranks

sycamorex 02-12-2009 12:45 PM

Quote:

Originally Posted by rankmar (Post 3441247)
Thanks H_TeXMeX_H, please what do you mean by file-temp and new? Are they folders?


regards

ranks

'new' is a folder and file-temp is just the name of a temporary file that will store the contents of file1. You need to store it somewhere temporarily as the command 'mv new/file2 file1' will erase the contents of file1.

Then those 2 commands:
Code:

bash-3.1$ cp new/file-temp new/file2
bash-3.1$ rm -f new/file-temp

could actually be reduced to one command:

Code:

mv new/file-temp new/file2
[edit]: I did not notice the requirement of rm command.


All times are GMT -5. The time now is 08:54 PM.