shell scripts howto

Any other programming language that is not covered in the Web Scripting category.

shell scripts howto

Postby crack0 on Wed Mar 29, 2006 10:15 pm

This is article about how to do some script programing on linux Wink



1. any text editor like vi , pico etc ...

2. after writing shell script set execute promission for your script like follows

examples:

# if you srcipt is called hello than

Code:
Code: Select all
$ chmod +x hello
$ chmod 755 hello

|||
|||
||----- this (5) will set read , execute for other
||
||-------- this (5) will set read , execute for group
|
|------------- this (7) will set read , write , execute promissions for owner


3. to execute your shell script use

examples:
Code:
Code: Select all
$ bash
$ sh
$ ./hello


now lets write some easy script Smiley

type this in your editor

Code:
Code: Select all
# clears the screen
# echos I made script on screen
clear
echo "hi to all"

save this as hello

now set promissions like
Code:
Code: Select all
chmod +x hello
chmod 755 hello


and now run script

do this
Code:
Code: Select all
./hello

new script

Write following shell script, save it, execute it and note down the it's output.
Code:
Code: Select all
clear
echo "Hello $USER" # say hello to active user
echo "Today is \c ";date # print's date
echo "Number of user login : \c" ; who | wc -l # number of logins
echo "Calendar" # calendar
cal
exit 0


This is simple howto for shell scripts Smile

If there are errors or something correct me Wink

I belive it will be usefull to other members Smile
Last edited by crack0 on Sun Oct 01, 2006 1:23 pm, edited 1 time in total.
Remember happiness can be found even in the darkest of times who only remembers to turn on the light
crack0
Regular
Regular
 
Posts: 111
Joined: Sun Dec 18, 2005 7:48 pm
Location: serbia

Postby cons00m on Fri Mar 31, 2006 5:02 am

Code: Select all
$ bash
$ sh
$ ./hello


You dont need to bash and then sh cause all you're doing is running a shell within a shell within a shell. If you want your shell scripts to run through a certain app/program use something like:

Code: Select all
#!/usr/bin/perl


You see that alot at the beginning of perl scripts or:

Code: Select all
#!/usr/bin/sh
#!/usr/bin/awk


You can also pass flags/command line arguments like to run a perl script through perl with warnings on you'd do

Code: Select all
#!/usr/bin/perl -w


Also if you want to see more enviroment varibles like the

Code: Select all
echo "Hello $USER" # say hello to active user


used in your example type:

Code: Select all
env


theres alot more to shell scripting, if you're really curious read the bash manpage regarding loops and etc.

~c
cons00m
Contributer
Contributer
 
Posts: 34
Joined: Thu Mar 23, 2006 12:36 am

Postby kotuk on Sun Apr 16, 2006 10:45 am

Hey,
what commands should I use to send a mail with attachment?
thanks
kotuk
Contributer
Contributer
 
Posts: 24
Joined: Sat Apr 15, 2006 7:42 pm

Postby ravenhair on Mon Apr 17, 2006 4:33 pm

kotuk wrote:Hey,
what commands should I use to send a mail with attachment?
thanks


should be it's own topic really kotuk, but what your looking for is:

uuencode $file |mail -s "mail subject" [email protected]
ravenhair
Regular
Regular
 
Posts: 80
Joined: Tue Nov 08, 2005 5:51 pm

Postby dlx on Wed May 31, 2006 12:42 pm

try usig "pine" to send and manage emails from a shell.. it has its own "gui" to get rid of commandline switches \o/
\o/
dlx
Contributer
Contributer
 
Posts: 37
Joined: Wed May 31, 2006 11:57 am
Location: .de

Postby Rommaster on Wed May 31, 2006 5:02 pm

Post Stickied. Cool
SwiftIRC - irc.swiftirc.net - #phoenix
Phoenix-Network

(\__/)
(='.'=) This is Bunny. Copy and paste bunny into your
(" )_(" ) signature to help him gain world domination.
Rommaster
Phoenix Veteran
Phoenix Veteran
 
Posts: 960
Joined: Thu Mar 10, 2005 1:05 pm
Location: The Netherlands

Postby Asbestos on Wed Jun 07, 2006 8:26 am

Thanks. This is probably the most intricate and useful how-to I've ever read on these boards.

Well done.
Asbestos
Contributer
Contributer
 
Posts: 38
Joined: Mon May 22, 2006 1:43 am
Location: Brisbane, Australia

Postby NightHunter on Tue Jun 20, 2006 10:14 pm

Thank you, I've been looking for a easy tutorial for a while now.
NightHunter
Newbie
Newbie
 
Posts: 16
Joined: Tue Jun 20, 2006 9:13 pm

Postby peterskate on Tue Jun 20, 2006 11:05 pm

Confused but to me this is not so easy : Rolling Eyes
peterskate
Newbie
Newbie
 
Posts: 15
Joined: Thu Apr 13, 2006 5:22 pm

Postby bugalinux on Wed Aug 02, 2006 2:44 pm

ravenhair wrote:
kotuk wrote:Hey,
what commands should I use to send a mail with attachment?
thanks


should be it's own topic really kotuk, but what your looking for is:

uuencode $file |mail -s "mail subject" [email protected]


Just a small modification it is rather that :

uuencode $file $file |mail -s "mail subject" [email protected]

Note : uuencode is provided by sharutils package (if you don't want to compile it)
Image
bugalinux
Contributer
Contributer
 
Posts: 52
Joined: Sat Aug 20, 2005 1:44 pm
Location: Paris

Postby bugalinux on Wed Aug 02, 2006 2:49 pm

dlx wrote:try usig "pine" to send and manage emails from a shell.. it has its own "gui" to get rid of commandline switches \o/


You can do more with pine but, personnaly I prefer mutt (just enough efficient for me, especially for local mailboxes).
Image
bugalinux
Contributer
Contributer
 
Posts: 52
Joined: Sat Aug 20, 2005 1:44 pm
Location: Paris

Postby Anto on Sun Aug 13, 2006 9:45 am

useful thinks ......

i got something new 2day
Anto
Newbie
Newbie
 
Posts: 10
Joined: Tue May 02, 2006 1:07 pm

sweet

Postby bondreal on Wed Sep 13, 2006 12:16 am

you have out togather alot of good information here
bondreal
Newbie
Newbie
 
Posts: 14
Joined: Tue Sep 12, 2006 11:49 pm

Postby bwolf on Fri Oct 06, 2006 2:38 am

Very interesting information guys, well done Smile.

I have yet to become a pro a shell scripting, but takes time and patience.

Note: I know this been here for awhile, but never saw it before lol
bwolf
Contributer
Contributer
 
Posts: 27
Joined: Sat Mar 18, 2006 3:23 am
Location: Ontario, Canada

Postby TomH on Fri Oct 06, 2006 8:57 am

Some good info here, im going to sit and have a play when i have a bit of spare time Very Happy
TomH
Contributer
Contributer
 
Posts: 40
Joined: Tue Jun 13, 2006 8:32 pm

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron