Jump to content

Passing files as parameters


Recommended Posts

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:

 

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

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:

uploadAC *.jpg

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

Link to comment
Share on other sites

You could substitute and use $1 in the script, but this would only be able to take one value. Eg:

 

cp $1 /home/username

 

for example in the script, and then when you type the script name:

 

uploadAC *.jpg

 

assuming the script is called uploadAC, then *.jpg would be in place of the $1. Or whatever you happened to type. However, as I said, you could only pass one parameter, not multiples with my example.

Link to comment
Share on other sites

Guest Hobbletoe

As Ian said, you'll have to do this in a script as you can not use variable substitution in an alias, nice as that might be. You can create the script, then make an alias to it though. Your script would be something as simple as ...

 

#!/bin/bash

scp $1 foo@server:/path/to/upload

exit

 

then you would just alias your script ...

 

alias uploadAC='~/scripts/uploadAC'

Edited by Hobbletoe
Link to comment
Share on other sites

ok it doesnt has to be a one liner, what I want is to explain how the value of a parametar can be inserted into the script. I got this answer from other forum.

#!/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

 

Here they used a temporary file to store filenames so I could send more than one file. That is what I wanted. The option that you present me is a bit different since you use fewer lines. What I need to understand is what is $1 is it a special variable, how is it populated by the parameter if there is no 'catcher'.

Link to comment
Share on other sites

Guest Hobbletoe

$1 represents the first argument supplied on the command line.

 

$2 represents the second argument supplied on the command line.

 

...

 

$9 represents the ninth arugement supplied on the command line.

 

So, if you have a simple script such as ...

 

#!/bin/bash

echo Argument 1:  $1

echo Argument 2:  $2

exit

 

and run it as

 

./test.sh abc xyz

 

You'd get

 

Argument 1:  abc
Argument 2:  xyz

 

So, if you were to run the updateAC script as

 

updateAC *jpg

 

when the script runs, it will substitute in *jpg for $1 as it is the first variable.

 

scp $1 foo@server:/path/to/update

 

becomes

 

scp *jpg foo@server:/path/to/update

 

I'm not sure what you mean by a "catcher", but I'll assume that you mean where they are shuffling all of those file names off into a file. It wouldn't be needed because by use of the $1 variable, you'd be telling scp to get all of the *jpg files.

 

 

You could also do something such as

 

#!/bin/bash

for i in $(ls $1)
do
 scp $i foo@server:/path/to/update
done

exit

 

thereby by-passing the need for a file. Here, $i becomes each of the files listed by ls $1 in turn (ie the first time through the loop it might be aaa.jgp, the second time bbb.jpg ...) [The $() construct in bash allows you to run a command within a command basically, and is synonymous with the use of backtics around a command. And as we want to list the name of each file that follows whatever you supply as the first argument of the script, it ends up being $(ls $1) or, we could have used backtics such as `ls $1`].

 

You might want to look at the Advanced Bash Scripting Guide out at The Linux Documentation Project. It has a lot of very good information, and a lot of examples.

Link to comment
Share on other sites

perhapss this could be usefull as well:

#!/bin/bash

until [ -z "$1" ]
do
	echo -n "$1" # do what you want with "$1"
	shift
done

exit 0

 

Edit:

i see that you want explanation.

$1 ... ${XX} are so called "positional parameters" which are the command-line arguments as they are separated by white space (unless quoted). "shift " command throws away the first argument in the list, so that $1 disappears, $2 beccomes $1, $3 -> $2, etc.

 

until ... ; do ; done is a loop.

 

[-z "$1"] checks if the argument exists. If it doesn't, the loop ends, since this is the condition for the loop to run.

 

In the form I gave it, test.sh * just lists the current dir contents

Edited by uralmasha
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...