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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-21-2012, 06:20 AM
|
#1
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Rep:
|
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.
|
|
|
05-21-2012, 06:37 AM
|
#2
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
What have you tried before? Can you perform the steps at the command prompt?
|
|
|
05-21-2012, 11:26 AM
|
#3
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Original Poster
Rep:
|
Quote:
Originally Posted by catkin
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
|
|
|
05-21-2012, 12:42 PM
|
#4
|
Moderator
Registered: May 2001
Posts: 29,415
|
Quote:
Originally Posted by ust
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.
|
|
|
05-21-2012, 02:08 PM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,022
|
Also remembering that a script is just what you enter on the command line put in a file
|
|
|
05-21-2012, 11:50 PM
|
#6
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Original Poster
Rep:
|
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 ?
|
|
|
05-22-2012, 12:52 AM
|
#8
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Assuming you have a recent enough version of bash, the regex comparison operator =~ will probably be useful.
|
|
|
05-22-2012, 01:35 AM
|
#9
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Original Poster
Rep:
|
Quote:
Originally Posted by chrism01
|
thanks your suggestion, but I have a bit urgent to fix it , can provide the hits ?
thx
|
|
|
05-22-2012, 12:29 PM
|
#10
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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 12:33 PM.
Reason: added some advice
|
|
|
All times are GMT -5. The time now is 06:26 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|