LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   scripting help (https://www.linuxquestions.org/questions/linux-newbie-8/scripting-help-152342/)

garretwp 03-01-2004 04:53 PM

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

lsmith 03-01-2004 05:06 PM

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!

lsmith 03-01-2004 05:07 PM

.. dup post

Tinkster 03-01-2004 05:16 PM

Quote:

Originally posted by lsmith
This only lets you alias a single command though (afaik)
Not quite ...

you can do something like
Code:

alias blah='ls -l | grep'
Now you can do:
Code:

tink@diggn> blah mail
-rwx------    1 tink    users      125378 2003-04-08 14:04 mail.jpg
-rwx------    1 tink    users        375 2003-06-15 11:10 mail.txt
-rwx------    1 tink    users        920 2003-06-15 11:10 mail.txt.asc
-rwx------    1 tink    users        188 2003-06-14 10:10 mail.txt~
-rwx------    1 tink    users        263 2003-07-10 13:07 mail_tmp
-rw-r--r--    1 tink    users        1739 2004-01-10 09:01 mails.txt
-rw-r--r--    1 tink    users      39460 2004-01-10 08:58 mails.txt~
-rwx------    1 tink    users      365231 2003-02-28 09:10 qmail-1.03-52.i386.rpm
-rwx------    1 tink    users      428665 2003-02-19 11:11 winmail.dat
drwx------  12 tink    users        1440 2003-03-05 13:00 xfsmail


Cheers,
Tink

garretwp 03-01-2004 07:17 PM

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

slakmagik 03-01-2004 07:43 PM

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. ;)

garretwp 03-01-2004 08:15 PM

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

lsmith 03-01-2004 08:23 PM

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.

slakmagik 03-01-2004 08:30 PM

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.

garretwp 03-01-2004 10:18 PM

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

Tinkster 03-01-2004 10:40 PM

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.