LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-01-2004, 04:53 PM   #1
garretwp
Member
 
Registered: Jan 2004
Posts: 46

Rep: Reputation: 15
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

Last edited by garretwp; 03-01-2004 at 04:56 PM.
 
Old 03-01-2004, 05:06 PM   #2
lsmith
Member
 
Registered: Feb 2004
Posts: 40

Rep: Reputation: 15
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!

Last edited by lsmith; 03-01-2004 at 05:08 PM.
 
Old 03-01-2004, 05:07 PM   #3
lsmith
Member
 
Registered: Feb 2004
Posts: 40

Rep: Reputation: 15
.. dup post
 
Old 03-01-2004, 05:16 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 03-01-2004, 07:17 PM   #5
garretwp
Member
 
Registered: Jan 2004
Posts: 46

Original Poster
Rep: Reputation: 15
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
 
Old 03-01-2004, 07:43 PM   #6
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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.

Last edited by slakmagik; 03-01-2004 at 07:47 PM.
 
Old 03-01-2004, 08:15 PM   #7
garretwp
Member
 
Registered: Jan 2004
Posts: 46

Original Poster
Rep: Reputation: 15
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
 
Old 03-01-2004, 08:23 PM   #8
lsmith
Member
 
Registered: Feb 2004
Posts: 40

Rep: Reputation: 15
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.
 
Old 03-01-2004, 08:30 PM   #9
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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.
 
Old 03-01-2004, 10:18 PM   #10
garretwp
Member
 
Registered: Jan 2004
Posts: 46

Original Poster
Rep: Reputation: 15
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
 
Old 03-01-2004, 10:40 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
new to scripting mifan Linux - Newbie 2 08-17-2005 12:10 PM
another help with scripting?! ice99 Programming 2 08-09-2005 08:56 AM
Need help scripting Tamara Programming 7 06-06-2005 02:06 AM
Scripting Help Please Jazinator Linux - Newbie 7 10-17-2004 06:35 PM
scripting bforest Linux - Newbie 4 05-11-2004 02:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration