Scripts 1
Scripts 1
1. Shell Commands
3. File Commands
1) Exit
2) Log out
3) echo
4) echo $PATH
5) Cal
6) Cal 2009
8) Date
a) Date +%m
9) who
10) tty
11) wall
1) ps
This command list all the processes that are currently running giving
details such as process id, terminal, time and command.
2) pstree
3) pstree -p
4) pgrep
For eg pgrep date:- This will show the process ID of the process. If
we use option –l then in addition to PID it also shows process name.
5) top
This command displays all the top processes. It give more details than ps
command. It refreshes all the process after every five seconds.
6) kill PID
7) kill % job_number
8) kill all
This command uses the name of the process to kill the process instead of
PID.
9) pkill
10) skill
11) ctrl c
12) jobs
It lists all the jobs running on the background and foreground in groups.
13) nice
14) renice
15) &
This operator will not allow user to log out while the jobs are running. This
is shell operator used to run a process in background.
3.File Commands
2) Change Directory(cd)
Changing the current directory
5) ls
Display all files
6) cp
Copying a file
7) rm
Deleting file
8) mv
Moving & renaming files
9) more
Display data one screen full at a time
10) less
Similar to more but we can move upward also
11) head
Shows first ten lines of a file
12) tail
Shows last ten lines of a file
13) uptime
Display how long the system has been running how many users are
currently logged on, system load average for the percentage cpu
utilization, in last one 5, 15 minutes.
14) file
Displays the file type
18) Common(comm.)
This command displays the common content of the file.
20) tar
The archival program with the following options:
a) tar-x : Extract files from archive
b) tar-t : Display files in archive
c) tar-c : Create the archive
23) mount
Attachment of the file or device
24) umount
Deatchments of a file or device
4. Write a shell script that simply displays a Welcome message.
OUTPUT:
$ ./test
echo”User Calender”
cal mm yyyy
echo “DATE”
date
echo ”Process Ststus”
ps
OUTPUT
$ sh linux1.sh
User Calender
November 2009
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
DATE
Tue Nov 17 22:08:48 PST 2009
Process Status
PID TTY TIME CMD
3239 pts/0 00:00:00 bash
3585 pts/0 00:00:00 sh
3622 pts/0 00:00:00 ps
6. Write a shell script to print the multiplication table of any number.
OUTPUT:
$ ./prg1
Enter a number : 19
The Table is :
19 * 1 = 19
19 * 2 = 38
19 * 3 = 57
19 * 4 = 76
19 * 5 = 95
19 * 6 = 114
19 * 7 = 133
19 * 8 = 152
19 * 9 = 171
19 * 10 = 190
7. Write a shell script to enter the age and declare it on the screen.
OUTPUT :
$ ./ prg2
Enter your age:
21
OUTPUT:
$ ./ prg3
Enter the number
6
OUTPUT:
$ ./ prg4 file1
Number of Words= 20
Number of Characters= 79
10. Write a shell script to print Fibonacci series.
OUTPUT:
$ ./ prg5
Enter the no. of terms required in the series 7
The series is :
0
1
1
2
3
5
8
11. Write a shell script to sort a given list of integers
OUTPUT:
$ ./ prg6
OUTPUT:
$ ./ prg7
Enter a number
43
Is an Odd Number
13. Write a shell script to calculate simple interest.
OUTPUT:
$ ./ prg8
Enter Principal
10000
Enter rate of interest
5
Enter Time period(in years)
3
Interest is: 1500
14. Write a shell script to generate odd numbers between 1 and 50.
i=1
while [ $i –le 50]
do
echo –n $i
i=’expr $i + 2’
done
OUTPUT:
$ ./ prg9
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
15. Write a shell script to accept a number from user, n and display square of all
numbers from 1 to n.
OUTPUT
$ sh num.sh
Enter number: 9
Square of 9 is: 81
OUTPUT:
$ ./ prg11
The Series is :
1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 13 12 14 13 15 14 16 15 17 16
18 17 19 18 20 19 21 20 22 21 23 22 24 23 25 24 26 25 27 26 28 27 29 28 30
29 31 30 32 31 33 32 34 33 35 34 36 35 37 36 38 37 39 38 40 39 41 40 42 41
43 42 44 43 45 44 46 45 47 46 48 47 49 48 50 49 51 50 52 51 53 52 54 53 55
54 56 55 57 56 58 57 59 58 60 59 61 60 62 61 63 62 64 63 65 64 66 65 67 66
68 67 69 68 70 69 71 70 72 71 73 72 74 73 75 74 76 75 77 76 78 77 79 78 80
79 81 80 82 81 83 82 84 83 85 84 86 85 87 86 88 87 89 88 90 89 91 90 92 91
93 92 94 93 95 94 96 95 97 96 98 97 99 98 100
17. Write a shell script to check if a given string is a palindrome or not (without
reversing the string).
echo "Enter the String :"
read str
len=`expr $str | wc -c`
len=`expr $len - 1`
mid=`expr $len / 2`
flag=1
m=1
while [ $m -le $mid ]
do
c=`echo $str | cut -c$len`
p=`echo $str | cut -c$m`
if [ $c != $p ]
then
flag=0
break
fi
m=`expr $m + 1`
len=`expr $len - 1`
done
if [ $flag -eq 1 ]
then
echo "The string is a Palindrome"
else
echo "The string is not a Palindrome"
fi
OUTPUT:
$ ./ prg12
OUTPUT:
$ ./ prg13