LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-08-2009, 12:33 PM   #1
rankmar
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Rep: Reputation: 0
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

Last edited by rankmar; 02-08-2009 at 12:44 PM.
 
Old 02-08-2009, 12:48 PM   #2
makuyl
Senior Member
 
Registered: Dec 2004
Location: Helsinki
Distribution: Debian Sid
Posts: 1,107

Rep: Reputation: 54
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.
 
Old 02-08-2009, 01:24 PM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
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'.
 
Old 02-08-2009, 03:03 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,073

Rep: Reputation: 384Reputation: 384Reputation: 384Reputation: 384
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.
 
Old 02-08-2009, 05:51 PM   #5
rankmar
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 02-09-2009, 03:39 AM   #6
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Rep: Reputation: 26
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
 
Old 02-09-2009, 10:18 AM   #7
makuyl
Senior Member
 
Registered: Dec 2004
Location: Helsinki
Distribution: Debian Sid
Posts: 1,107

Rep: Reputation: 54
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.

Last edited by makuyl; 02-09-2009 at 10:20 AM.
 
Old 02-09-2009, 03:36 PM   #8
rankmar
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 02-10-2009, 01:07 AM   #9
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Rep: Reputation: 26
Sure....it is great to see ur spirits.
Good luck !!
 
Old 02-12-2009, 09:04 AM   #10
rankmar
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 02-12-2009, 10:05 AM   #11
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
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.
 
Old 02-12-2009, 12:11 PM   #12
rankmar
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks H_TeXMeX_H, please what do you mean by file-temp and new? Are they folders?


regards

ranks
 
Old 02-12-2009, 12:45 PM   #13
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,823
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
Quote:
Originally Posted by rankmar View Post
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.

Last edited by sycamorex; 02-12-2009 at 12:49 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
KDEPrint virtual printer that pipes content to custom shell script? hexstar Linux - Software 1 07-03-2007 09:17 AM
shell script for changing a txt file content Flobsi Linux - Newbie 3 10-06-2006 03:10 AM
shell script for changig txt-file content Flobsi Programming 1 10-05-2006 05:46 PM
modify content in a file using shell script fjkum Linux - Newbie 2 08-03-2006 04:46 AM
Shell script: substitute a file's content according to a map? Chowroc Programming 11 11-15-2005 04:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration