LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Two questions about scripts (https://www.linuxquestions.org/questions/linux-newbie-8/two-questions-about-scripts-552840/)

gubak 05-10-2007 05:46 AM

Two questions about scripts
 
Hi!
I've created a script (it doesn't matter what it do). I want to run that script from everywhere (from /home directory from /etc etc.)with a single command. Is it possible? How?
Is it possible to give parameter for script?

Thanks

druuna 05-10-2007 05:56 AM

Hi,

If the script itself is placed in a directory that is in the PATH environment, you can access it from every point. You could encounter permission problems if the dir your are running the script from does not allow you to read/write (that depends also on what the script does).

If the script itself is placed in a directory that is nor in the PATH environment you can run it by including the full path to that script: /path/to/script/scriptname. But this could be seen as not being a single command....

Hope this helps.

jschiwal 05-10-2007 06:00 AM

Scripts you write yourself are often saved in ~/bin/. Add '$HOME/bin' to your PATH variable in ~/.profile.

gubak 05-10-2007 06:25 AM

Quote:

Originally Posted by druuna
Hi,

If the script itself is placed in a directory that is in the PATH environment, you can access it from every point. ...

What means PATH environment?
Do you know maybe how to add parameters to script? For example my script name is "premission". If I want it to run I type
Quote:

./premission
I want to give the parameter "62.5.3.15" (an IP of a machine)to the script. How can I do this?

ethics 05-10-2007 08:06 AM

is the parameter variable? you could use $1 as the variable in your script (whatever it is doing) and it will take it as a parameter so you'd type
Code:

permission <IP>
and it would use the IP in the script.

druuna 05-10-2007 09:03 AM

Hi,

Quote:

Originally Posted by gubak
What means PATH environment?(an IP of a machine)to the script.

The PATH variable holds all directories that are searched when you call a program (or script).

Try echo $PATH from within a terminal and you'll see a lot of directories separated by a :. This makes it possible to run a program/script from any location, without having to remember and add the full path to that program/script. It's a lot easier to run ls instead of /bin/ls.

You can manipulate this (as jschiwal already wrote) and add your own directory (~/bin is often used to place home-made scripts). The standard PATH is set in /etc/profile, personal additions can be added in ~/.profile.

To add a directory to the PATH variable you something like this: export PATH=$PATH:/your/new/dir

Hope this clears things up a bit.


All times are GMT -5. The time now is 02:42 PM.