LinuxQuestions.org
Review your favorite Linux distribution.
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 05-21-2012, 05:20 AM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Rep: Reputation: 30
New script


I would like to have a new script to perform the below task .


I have a file called dummy.txt , the content of this file is as below .

aaa.prn
bbb.prn
ccc.prn

I would like to have a script which can do

1) check dummy.txt
2) then copy these files ( but with other extension .xls ) from a specify directory to another server
eg. scp the files /tmp/aaa.xls , /tmp/bbb.xls , /tmp/ccc.xls to /tmp of another server


can advise what can i do ?

thanks in advance.
 
Old 05-21-2012, 05:37 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
What have you tried before? Can you perform the steps at the command prompt?
 
Old 05-21-2012, 10:26 AM   #3
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by catkin View Post
What have you tried before? Can you perform the steps at the command prompt?
yes , I can perform the steps at the command prompt , can advise how to write the script ?

thx
 
Old 05-21-2012, 11:42 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,353
Blog Entries: 55

Rep: Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541
Quote:
Originally Posted by ust View Post
yes , I can perform the steps at the command prompt
I think that actually was a hint for you to post actual code. It's more efficient that way to see where you get stuck and help you with that part.
 
Old 05-21-2012, 01:08 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Also remembering that a script is just what you enter on the command line put in a file
 
Old 05-21-2012, 10:50 PM   #6
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
thx reply ,

i just tried it , it seems works .


for file in $(< dummy.txt); do

base=${file%.prn}

scp /tmp/$base.xls remote:/tmp

done

But I have another requirement -

actually , the file name in dummy.txt have two type of prefix , one is xxx ( eg. xxxfile.prn ) , another one is yyy ( eg. yyyfile.prn ) , if the file is xxx , then copy to /tmp of another server , if the file is yyy then copy to /tmp1 of another server , can advise what can i do ?
 
Old 05-21-2012, 11:10 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
You should bookmark these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

You'll save yourself a lot of time in the long run if you learn bash and associated tools.

Try the Substring Extraction section here http://tldp.org/LDP/abs/html/string-manipulation.html to extract eg xxx or yyy from the $base & then match it.
 
Old 05-21-2012, 11:52 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Assuming you have a recent enough version of bash, the regex comparison operator =~ will probably be useful.
 
Old 05-22-2012, 12:35 AM   #9
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by chrism01 View Post
You should bookmark these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

You'll save yourself a lot of time in the long run if you learn bash and associated tools.

Try the Substring Extraction section here http://tldp.org/LDP/abs/html/string-manipulation.html to extract eg xxx or yyy from the $base & then match it.
thanks your suggestion, but I have a bit urgent to fix it , can provide the hits ?

thx
 
Old 05-22-2012, 11:29 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

You might want to try giving us some actual examples of filenames and directory trees to work with, rather than trying to describe them.

Anyway, your loop is very close, except: don't read lines with for!

Use a while+read loop instead. Or read the file into an array first with the mapfile command, and loop over that.

As for your new requirement, I suggest simply adding a case statement to the loop to test the naming pattern of each file, and run the appropriate command.

Code:
case $file in

	xxx*) <move file xxx command>  ;;
	yyy*) <move file yyy command>  ;;
	   *) echo "unknown filename"  ;;

esac
It would also be a good idea to add some intermediate tests too, to ensure that each file is actually available, as is the target, before trying to move it.


The Bash Guide is a good resource that covers all the basics of scripting. Check it out:
http://mywiki.wooledge.org/BashGuide

Last edited by David the H.; 05-22-2012 at 11:33 AM. Reason: added some advice
 
  


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
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 01:27 PM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 03:05 AM
How to execute a ssh script on Linux server from Windows through a bat script? wanna13e Programming 13 10-23-2009 02:41 AM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 08:40 AM


All times are GMT -5. The time now is 01:15 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