scripting help
As of course you can see i am new to all this. But my knowledge is growing as i learn more and more :). My issue here is, i want to be able to make a command. For example i want to say make a command that does the same as clear but instead of typing clear, i can just type cls. Another example is run more than one command after another. Say rn the uptime command and than the netstat command. Can someone show me how to do this? thanks
Garrett |
There are two ways.
The first is a simple alias. It gives one command a new name so you can type the alias to do the same thing. Under bash: alias cls=clear Then you can just type 'cls' and it will run clear. This only lets you alias a single command though (afaik), for more complicated shortcuts you will have to create a script file. Open a new text file and enter the following: #!/bin/bash # This is a comment line clear The first line tells linux to use the bash interpreter to read this file. Save the file, named 'cls' You now have to change the permissions so that it is executable: chmod u+x cls now you can type 'cls' and will execute that file. If you want to be able to execute it from anywhere, you have to copy it to somewhere that appears in your PATH variable. Thats about it..... of course the script files can be as big as you like, and can take arguments and options just like all the regular command line programs. Script files are used in the linux system for all kinds of goodness, including the boot sequence etc etc. Good luck! |
.. dup post
|
Quote:
you can do something like Code:
alias blah='ls -l | grep' Code:
tink@diggn> blah mail Cheers, Tink |
thanks for the help guys, but i have a problem. I did as you said and within that directory i type in that command and no go. it just says cls: command not found. Any ideas?
Garrett |
If you mean you wrote the file, try './cls' if the current directory isn't on the path, either as 'current directory' or the full path. (And this assumes you remembered to make it executable.) :) If you created the alias from the prompt, I dunno what's wrong, unless you tried it in another xterm or terminal. To make the alias take effect generally, it needs to be in the bash configs. My ~/.bash_profile has
alias c='clear' and ~/.bashrc is a symlink to it. -- Oh, you can always do ctlrl-l in character mode and usually in a terminal emulator if that'd be easier. ;) |
I was tring to do the text portion of it. Adding
#!/bin/bash # This is a comment line clear And than changing the rights to it and making it an executable. But get command not found. Garrett |
If you try
./cls instead, then it should work, if the executable permissions have been set. (Use ls -l to see the permissions.) If this works, then the problem was that your PATH variable does not include the current directory, so it could not find the file. Do: echo $PATH to see the list of directories that will be searched, seperated by semicolons. You could try adding the current directory to the path variable: export PATH=$PATH:. and then try cls again (without the ./ in front) and it should find it. |
Yeah - and not that it's that big of a deal but you might choose not to add the current directory to your path (marginal security issue) but create a directory to hold your scripts and put that on the path. I do that just so I can find them more easily if I need to edit them and keep up with what's what and so on.
|
Thanks for the help, i am guessing any scrips made in a directory other than the ones being searched have to have ./name in order for it to work, but as well as be in the directory of that script? I just made the script and than copied that script to the /usr/bin directory. The command you gave about the export PATH=$PATH, does that alow me to add a directory with all my scripts in there to be searched?
Garrett |
Well, . (a period by itself) to the shell is a
reference to the current directory ... so, if you want to call the script using ./ you need to be in the same directory ... you can, however, reference it with ~/<name> if it lives in your home, and it should work from where ever you called it ... .., btw, is a parent directory ... so if you were in /usr/local/bin, and the script your're calling were in /usr/bin you could do ../../bin/<name> :} Cheers, Tink |
All times are GMT -5. The time now is 09:49 AM. |