LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   New script (https://www.linuxquestions.org/questions/linux-newbie-8/new-script-946113/)

ust 05-21-2012 05:20 AM

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.

catkin 05-21-2012 05:37 AM

What have you tried before? Can you perform the steps at the command prompt?

ust 05-21-2012 10:26 AM

Quote:

Originally Posted by catkin (Post 4683813)
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

unSpawn 05-21-2012 11:42 AM

Quote:

Originally Posted by ust (Post 4684028)
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.

grail 05-21-2012 01:08 PM

Also remembering that a script is just what you enter on the command line put in a file :)

ust 05-21-2012 10:50 PM

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 ?

chrism01 05-21-2012 11:10 PM

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.

catkin 05-21-2012 11:52 PM

Assuming you have a recent enough version of bash, the regex comparison operator =~ will probably be useful.

ust 05-22-2012 12:35 AM

Quote:

Originally Posted by chrism01 (Post 4684459)
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

David the H. 05-22-2012 11:29 AM

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


All times are GMT -5. The time now is 11:46 AM.