Reputation: 84
I'm trying to write a shell script which basically goes into a particular folder and performs some actions. The catch here is that the folder name is variable i.e. /path/to/variable
I was thinking, is it possible to run a shell script from command line and also define a variable in the same line? Something like:
./run.sh $id=456
When it runs, it takes the path as
/path/to/$id
and hence /path/to/456
.
Upvotes: 2
Views: 175
Reputation: 13394
You can run the script in the following style:
./run.sh 465
Than you can access the content of the parameter inside the script with $1
(1st parameter), $2
(2nd parameter) and so on.
To change the directory use cd /path/to/$1
BTW: $#
returns the count of parameters.
Another way is to use arguments, like here: http://www.linux.com/archive/feature/118031
So you can run your script like this: ./run.sh -d 465
(where -d stand for directory)
Upvotes: 4