Google Search

Google

What is the most suitable distro to use as a server?

Visit Us!

Google Groups
Kuantan Linux User Group
Visit this group

Threat Resource Center | Trend Micro

Sunday, April 18, 2010

How to write a simple shell script in Linux

Below are the simple steps to write a simple shell script in Linux machine. Before this I don't have any experience to write a script. Thanks to HP's Virtual Lab. Its really make my day ;-)


===========
Exercise 1
===========

1) Find what shells are available on your system.

[root@vmpc_rh804 root]# more /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/bash2
/bin/ash
/bin/bsh
/bin/tcsh
/bin/csh


2) Using an editor or the cat command, write a korn or bash script that:

* Comments what the script will do.
* Shows your current directory.
* Shows who is currently logged on.
* Prints the current date and time.

[root@vmpc_rh804 root]# cat > myscript
#! /bin/bash
#
# This script shows current directory
# then shows who is on the system
# then prints the date and time
#
pwd
who
date
Ctrl/D


3) Run the script by calling up the shell interpreter.

[root@vmpc_rh804 root]# bash myscript
/root
root :0 Apr 11 06:25
root pts/0 Apr 11 06:26
root pts/1 Apr 11 06:26
root pts/2 Apr 11 06:26
root pts/3 Apr 11 06:26
root pts/5 Apr 11 06:27
root pts/4 Mar 30 00:25 (16.158.13.22)
Tue Mar 30 00:55:01 EST 2010


4) Show how the script cannot be run as a command unless the file has execute privileges.

[root@vmpc_rh804 root]# ./myscript
-bash: ./myscript: Permission denied


5) Add execute privilege to the script file and try running it again.

[root@vmpc_rh804 root]# chmod +x myscript
[root@vmpc_rh804 root]# ./myscript
/root
root :0 Apr 11 06:25
root pts/0 Apr 11 06:26
root pts/1 Apr 11 06:26
root pts/2 Apr 11 06:26
root pts/3 Apr 11 06:26
root pts/5 Apr 11 06:27
root pts/4 Mar 30 00:25 (16.158.13.22)
Tue Mar 30 01:04:14 EST 2010




============
Exercise 2
============


1) [root@vmpc_rh804 root]# more /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/bash2
/bin/ash
/bin/bsh
/bin/tcsh
/bin/csh

[root@vmpc_rh804 root]# cat > myscript1
#!/bin/bash
#
#
ifconfig eth0
netstat -i


2) [root@vmpc_rh804 root]# bash myscript1
eth0 Link encap:Ethernet HWaddr 00:50:56:03:04:40
inet addr:172.16.0.40 Bcast:172.16.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b) TX bytes:168 (168.0 b)
Interrupt:10 Base address:0x10a0

Kernel Interface table
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 0 0 0 0 0 4 0 0 0 BMRU
eth1 1500 0 1335 0 0 0 1351 0 0 0 BMRU
lo 16436 0 98 0 0 0 98 0 0 0 LRU


3) [root@vmpc_rh804 root]# ./myscript1
-bash: ./myscript1: Permission denied


4) [root@vmpc_rh804 root]# chmod +x myscript1


5) [root@vmpc_rh804 root]# ./myscript1
eth0 Link encap:Ethernet HWaddr 00:50:56:03:04:40
inet addr:172.16.0.40 Bcast:172.16.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b) TX bytes:168 (168.0 b)
Interrupt:10 Base address:0x10a0

Kernel Interface table
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 0 0 0 0 0 4 0 0 0 BMRU
eth1 1500 0 1397 0 0 0 1395 0 0 0 BMRU
lo 16436 0 98 0 0 0 98 0 0 0 LRU

No comments:

Linux News Of The Day!

Powered By
widgetmate.com
Sponsored By
Credit Card Forum