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

Showing posts with label Script Thinggy. Show all posts
Showing posts with label Script Thinggy. Show all posts

Sunday, April 18, 2010

Use Variables with Scripts

Using an editor or the cat command, write a script that uses a variable. Note that this script uses the bash shell on LINUX, but you can also use the ksh shell with UNIX.


1) Set a value for the variable first using typeset then check it using set.

[root@vmpc_rh804 root]# typeset first=you
[root@vmpc_rh804 root]# set | more
BASH=/bin/bash
BASH_ENV=/root/.bashrc
BASH_VERSINFO=([0]="2" [1]="05b" [2]="0" [3]="1" [4]="release" [5]="i686-pc-linu
x-gnu")
BASH_VERSION='2.05b.0(1)-release'
COLORS=/etc/DIR_COLORS
.
.
.
first=you
hi=hello
langfile=/root/.i18n


2) Write a short Welcome script that uses a variable.

[root@vmpc_rh804 root]# cat > Welcome.bash
#!/bin/bash
#
first=Student
echo "Welcome to Linux $first"
Ctrl/D


3) Execute your script using the shell interpreter.

$ bash Welcome.bash
Welcome to Linux Student

Invoke a Script

Use various methods to invoke a script, and determine when it runs in your shell, and when a subshell is created.


1) Using an editor or the cat command, write a korn or bash script that:
* Executes the ps command.
* Echos the value of the shell variable hi.

[root@vmpc_rh804 root]# cat > do-ps.bash
#!/bin/bash
# This script displays active processes
#
ps
echo $hi


2) Set the value of hi to hello in your current command line shell.

[root@vmpc_rh804 root]# hi=hello


3) Execute your script using the shell interpreter.

[root@vmpc_rh804 root]# bash do-ps.bash
PID TTY TIME CMD
1311 pts/4 00:00:00 bash
1358 pts/4 00:00:00 bash
1365 pts/4 00:00:00 ps

** Note that it doesn't recognize the hi variable. This is because the variable is local to the shell in which it was defined. When this script is executed, a new shell is started that is outside your local shell. **


4) Add execute privilege to the script file and run it as a command from the current directory.

[root@vmpc_rh804 root]# chmod +x do-ps.bash
[root@vmpc_rh804 root]# ./do-ps.bash
PID TTY TIME CMD
1311 pts/4 00:00:00 bash
1367 pts/4 00:00:00 do-ps.bash
1374 pts/4 00:00:00 ps


5) Use the built-in source command to execute the commands in the script file.

[root@vmpc_rh804 root]# source do-ps.bash
PID TTY TIME CMD
1311 pts/4 00:00:00 bash
1375 pts/4 00:00:00 ps
hello
[root@vmpc_rh804 root]#


** Note that it is able to recognize the hi variable. This is because the source command runs this script in the local shell. **

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

Linux News Of The Day!

Powered By
widgetmate.com
Sponsored By
Credit Card Forum