Register
It is currently Tue Jul 29, 2014 6:44 am

Passing information to a script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Mon May 15, 2006 6:45 pm   

Joined: Mon Apr 24, 2006 2:39 pm
Posts: 30
Location: Villahermosa
Ok here is a doubt that I always had. I want to build a script but I want the script to be dinamic whnre is needed and static on the rest.

Here is an example, when I use scp let say i want to have a script to upload images. So the string will look like this:

Code:
scp *.jpg foo@server.com:~/path/to/upload


What I want is to insert files instead of the *.jpg, but at the same time variables might not help unelss they are pre assigned.

I want something like
Code:
alias uploadAC='scp $a foo@ser....

and use something like $a='file'l; uploadAC OR if there's a way to make the script intelligent enough so I can just put something like:
Code:
uploadAC *.jpg

I hope this explain what I where I want to get at. Thanks.


Top
 Profile WWW ICQ YIM  
 PostPosted: Mon May 15, 2006 8:54 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 663
Location: Des Moines, Iowa
I'm thinking your alias would be more than a one liner, so you would probably have to alias a function..... which would be a more in depth script that would do what you want.


Top
 Profile WWW  
 PostPosted: Mon May 15, 2006 8:56 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
bash is super intelligent... as long as you make it that way :wink:

so let's say you have a script named "uploadFile.sh"

inside that script you would have the following code...

Code:
#!/bin/bash

ls -1 "$1" > uploadList

until ! read cur_file
do
   scp $cur_file foo@server.com:~/path/to/upload
done < uploadList
rm -f uploadList

with that code.. you could call the script like...
uploadFile.sh "*.jpg"
or
uploadFile.sh "file1.jpg file2.jpg file3.jpg"
or
uploadFile.sh "file1.jpg"

i think you get the idea...
the "" around the file names is only really needed if you list more than one, or there is a space in the file name :)

enjoy


Top
 Profile  
 PostPosted: Mon May 15, 2006 9:56 pm   

Joined: Mon Apr 24, 2006 2:39 pm
Posts: 30
Location: Villahermosa
jbsnake wrote:
bash is super intelligent... as long as you make it that way :wink:

so let's say you have a script named "uploadFile.sh"

inside that script you would have the following code...

Code:
#!/bin/bash

ls -1 "$1" > uploadList

until ! read cur_file
do
   scp $cur_file foo@server.com:~/path/to/upload
done < uploadList
rm -f uploadList

with that code.. you could call the script like...
uploadFile.sh "*.jpg"
or
uploadFile.sh "file1.jpg file2.jpg file3.jpg"
or
uploadFile.sh "file1.jpg"

i think you get the idea...
the "" around the file names is only really needed if you list more than one, or there is a space in the file name :)

enjoy


Great, this is what I wanted to do. However this is what my newbie feathers show and I just glare with excitement not understanding a bit. Well not really, i want to figure out what you did.

So you put this line:
Code:
ls -1 "$1" > uploadList


So I assume uploadList here is a temporary file which will be populated by the "$1" which is another variable yet my first doubt is where it gets it's values from?

Then is the function itself:
Code:
until ! read cur_file


Here I am totally lost, where did the cur_file came from what does it has, is this like uploadList?

Code:
done < uploadList


Ok this line is simple, however I still dont get it, well I see that uploadList file is dumped into done. :)

Still great help I will look into my BASH tutorials.


Top
 Profile WWW ICQ YIM  
 PostPosted: Tue May 16, 2006 10:24 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
jza wrote:
Code:
#!/bin/bash

ls -1 "$1" > uploadList

until ! read cur_file
do
   scp $cur_file foo@server.com:~/path/to/upload
done < uploadList
rm -f uploadList

with that code.. you could call the script like...
uploadFile.sh "*.jpg"
or
uploadFile.sh "file1.jpg file2.jpg file3.jpg"
or
uploadFile.sh "file1.jpg"
-----------------------------------------------------------------------------------------
Great, this is what I wanted to do. However this is what my newbie feathers show and I just glare with excitement not understanding a bit. Well not really, i want to figure out what you did.


Answers are inline below, before the question in code with # to show my new comments:
jza wrote:

So you put this line:
Code:
# list the contents with one item per line
# and stick into a temp file we named uploadList
ls -1 "$1" > uploadList


So I assume uploadList here is a temporary file which will be populated by the "$1" which is another variable yet my first doubt is where it gets it's values from? # it gets it's value from how you call the script... check this out...

Then is the function itself:
Code:
# this begins your until loop...
# this line is saying "until you can't read any more into variable cur_file
until ! read cur_file


Here I am totally lost, where did the cur_file came from what does it has, is this like uploadList? # we just created it... it's a variable that holds a line from a file

Code:
# we are redirecting the contents of the file uploadList
# into the until loop that is attached to this done statement
done < uploadList


Ok this line is simple, however I still dont get it, well I see that uploadList file is dumped into done. # essentially... yes :) :)

Still great help I will look into my BASH tutorials.


don't forget to read all the tutorials i have at usalug.org under the scripting and shells section... if you don't have an account at usalug.org... create one... it's well worth it :wink:


Top
 Profile  
 PostPosted: Tue May 16, 2006 11:27 pm   

Joined: Mon Apr 24, 2006 2:39 pm
Posts: 30
Location: Villahermosa
ok so I finally run the script and well it didnt work, it gave me an error and the issue is:
Code:
ls -l "$1" > uploadList

The error was:
Code:
ls: : No such file or directory


I went to your site but really didnt got the explanation.


Top
 Profile WWW ICQ YIM  
 PostPosted: Wed May 17, 2006 9:18 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
did you have the script in the same directory as the jpgs?
if not... include the path...
uploadFile.sh "/path/to/files/*.jpg"

it seemed you wanted to run the script from within the directory of the pictures...


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP