scripts 1

27
INDEX S.NO PRACTICALS SIGN 1. Shell Commands 2. Shell Commands For Processes 3. File Commands 4. Script that displays a simple Welcome Message 5. Shell script to display calendar, date and process status. 6. Shell script to print the multiplication table of any number. 7. Shell script to enter the age and declare it on the screen. 8. Shell script to find factorial of a given number. 9. Shell script to count the no. of words and chars in a file. 10. Shell script to print Fibonacci series. 11. Shell script to sort a given list of integers 12. Shell script to find whether the entered number is even or odd. 13. Shell script to calculate simple interest. 14. Shell script to generate odd numbers between 1 and 50. 15. Shell script to accept a number from user, n and display square of all numbers from 1 to n.

Upload: nitindxt

Post on 18-Nov-2014

120 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Scripts 1

INDEX

S.NO PRACTICALS SIGN

1. Shell Commands

2. Shell Commands For Processes

3. File Commands

4. Script that displays a simple Welcome Message

5. Shell script to display calendar, date and process status.

6. Shell script to print the multiplication table of any number.

7. Shell script to enter the age and declare it on the screen.

8. Shell script to find factorial of a given number.

9. Shell script to count the no. of words and chars in a file.

10. Shell script to print Fibonacci series.

11. Shell script to sort a given list of integers

12. Shell script to find whether the entered number is even or odd.

13. Shell script to calculate simple interest.

14. Shell script to generate odd numbers between 1 and 50.

15. Shell script to accept a number from user, n and display square of all numbers from 1 to n.

16. Shell script to generate the series 1,3,2,4,3,5,4,6……100.

17. Shell script to check if a given string is a palindrome or not (without reversing the string).

18. Shell script to reverse a string.

1. SHELL COMMANDS

Page 2: Scripts 1

1) Exit

This command is used to exit from the shell.

2) Log out This command is used to log out from the shell.

3) echo

This command is used to print text on the screen.

4) echo $PATH

This command gives the path of the shell.

5) Cal This command shows the calendar of present month.

6) Cal 2009

This command shows the calendar of 2009.

7) Cal 2009 | move

This command shows the calendar of year 2009 pagewise and we have to press enter to see next page. “ | ” is known as the pipe. It transfer the output of Cal command to the move command.

8) Date

This command shows the current date.

a) Date +%m

This gives the number of current month.

b) Date +%n

This gives the name of the current month.

Page 3: Scripts 1

9) who

This command shows how many users are log in.

10) tty

This command shows the current terminal on which we are working

11) wall

This command broadcasts the message on all the terminals.

12) write system_number

This command send the message to a particular system specified by system number.

2. Shell Commands For Processes

1) ps

This command list all the processes that are currently running giving details such as process id, terminal, time and command.

Page 4: Scripts 1

2) pstree

This command lists all the processes in a tree structure.

3) pstree -p

This command in addition to displaying the processes in a tree structure also displays process ID.

4) pgrep

This command gives ID of the mentioned process.

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

It simply kills a process by mentioning its ID.

7) kill % job_number

This command kills the job specified by the job_number.

8) kill all

This command uses the name of the process to kill the process instead of PID.

9) pkill

This command kills a process by a small match.

For eg. Pkill -u user_1 user_2 process_name

This command kills the process specified by process_name of user_1 and user_2.

10) skill

Page 5: Scripts 1

This command is used to send a particular signal.

skill _ STOP user_name :- This command stops all user processes until we send continue signal.

11) ctrl c

This command interrupts the execution. Also known as break command.

12) jobs

It lists all the jobs running on the background and foreground in groups.

13) nice

This command is used by the administrator to set the priority of the processes.

14) renice

It changes the priority of the processes.

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

1) Print Working Directory (pwd) Checking the current working directory

2) Change Directory(cd)Changing the current directory

3) Make Direcotry (mkdir) Is use to create directory & subdirectories under the directory

Page 6: Scripts 1

4) Remove Directory (rmdir) Removing drectory

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) fileDisplays the file type

15) Word Count (wc) Counting the words , lines, characters.

16) Octal Display(od) Displays the octal representation of the file content

17) Compare (cmp) Comparing the two sorted files.

Page 7: Scripts 1

18) Common(comm.) This command displays the common content of the file.

19) gzip and gunzip Compressing and Decompressing the file

20) tar The archival program with the following options:

a) tar-x : Extract files from archiveb) tar-t : Display files in archivec) tar-c : Create the archive

21) zip and unzipCreates and archive as well as compressing the file

22) Change Mode (chmod)This command change the permission on the file

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.

echo "WELCOME TO THE SHELL SCRIPTS"

OUTPUT:

$ ./test

WELCOME TO THE SHELL SCRIPTS

Page 8: Scripts 1

5. Write a shell script to display calendar, date and process status. echo”User Calender” cal mm yyyy echo “DATE” date echo ”Process Ststus” psOUTPUT$ sh linux1.shUser Calender November 2009 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 1415 16 17 18 19 20 21

Page 9: Scripts 1

22 23 24 25 26 27 2829 30 DATE Tue Nov 17 22:08:48 PST 2009Process 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.

echo “Enter the number: “

read num //cmd to read user input

count=1 //user defined variable

echo “The Table is : “

while[$count-le 10] //checking the value of count continues till 10

do

ans= ’expr $count \* $num’ //multiplying the required numbers

echo $num “*” $count “=” $ans

count ‘expr $count + 1’

Page 10: Scripts 1

done

OUTPUT:

$ ./prg1

Enter a number : 19

The Table is :

19 * 1 = 1919 * 2 = 3819 * 3 = 5719 * 4 = 7619 * 5 = 9519 * 6 = 11419 * 7 = 13319 * 8 = 15219 * 9 = 17119 * 10 = 190

7. Write a shell script to enter the age and declare it on the screen.

echo “Enter your age: “;

read age

if [$age –le12]

then echo “You are a Child”

elif [$age –ge13 –a $age –le 19]

then echo “You are a Teenager”

elif [$age –ge 20 –a $age –le 30]

then echo “You are young”

elif [$age –ge 31 –a $age –le 60]

Page 11: Scripts 1

then echo “You are an Adult”

elif [$age –gt 60]

then echo “You are an Old Person”

fi //to end if

OUTPUT :

$ ./ prg2Enter your age:21

You are young

8. Write a shell script to find factorial of a given number. echo “ Enter the number”

read num

count =1

ans=1

while[$count –le $num]

do

echo $count

ans= ‘expr $ans \* $count’

Page 12: Scripts 1

count=’expr $count +1’

done

echo “The factorial of ” $num “is: “ $ans

OUTPUT:

$ ./ prg3Enter the number6

The factorial of 6 is 720

9. Write a shell script to count he no. of words and chars in a file. echo “Enter the data”

cat>file1

echo “No. of words: “

wc –w file1

echo “No. of characters”

wc-c file1

echo

Page 13: Scripts 1

OUTPUT:

$ ./ prg4 file1

Number of Words= 20 Number of Characters= 79

10. Write a shell script to print Fibonacci series. echo “Enter the no. of terms required in the series”

read num

i=0; j=1;k=2

if [$num –le 1] ; then

echo “No. of terns should be atleast 2”

else

echo $i

echo $j

Page 14: Scripts 1

while [$k –lt $num]

do

k=`expr $k + 1`

r=`expr $i + 1`

echo $r

i=$j

j=$r

done

fi

OUTPUT:

$ ./ prg5Enter the no. of terms required in the series 7

The series is :0112358

11. Write a shell script to sort a given list of integers echo “Enter the list of integers

cat>file1 // list of integers will be stored in a file

echo :Sorted List is :-“

sort –n file1 // cmd to sort the contents of the file given

OUTPUT:

$ ./ prg6

Page 15: Scripts 1

Sorted list is:-6 10 13 14 17 19 14 19 25 28

12. Write a shell script to find whether the entered number is even or odd.

echo “Enter a number”

read n

if [ $n % 2 –eq 0]

then echo “Its an Even number’

else

echo “Its an Odd number”

fi

Page 16: Scripts 1

OUTPUT:

$ ./ prg7Enter a number43Is an Odd Number

13. Write a shell script to calculate simple interest.

echo “Enter Principal”

read p

echo “Enter rate of interest “

read r

echo “Enter Time period(in years)”

read t

s= ‘expr $p \* $r \* $t’

s= ‘expr $s /100’

echo “Simple Interest is : “ $s

Page 17: Scripts 1

OUTPUT:

$ ./ prg8Enter Principal10000Enter rate of interest5Enter Time period(in years)3Interest 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:

Page 18: Scripts 1

$ ./ prg9

Odd numbers between 1 and 50:

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.

echo “Enter number: \c”

read n

i=1

while [ $i –le $n]

do

m=`expr $i \* Si`

echo “Square of “ $i “ is:-“ $m

i= `expr $i + 1`

done

OUTPUT

Page 19: Scripts 1

$ sh num.shEnter number: 9Square of 9 is: 81

16. Write a shell script to generate the series 1,3,2,4,3,5,4,6……100.

i=1

j=3

while [$j –le 100]

do

echo $i

i=`expr $i + 1`

echo $j

j=`expr $j + 1`

done

Page 20: Scripts 1

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`

Page 21: Scripts 1

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

Enter the String :LEVELThe string is a Palindrome

Page 22: Scripts 1

18. Write a shell script to reverse a string.

echo -n "Enter a string to reverse: "

read string

echo "$string"

len=`echo -n $string | wc -c`

echo "Number of character is: $len"

while [ $len -gt 0 ]

do

rev=$rev`echo -n $string | cut -c $len`

len=`expr $len - 1`

done

echo "The reverse string is:$rev"

Page 23: Scripts 1

OUTPUT:

$ ./ prg13

Enter a string to reverse: india

Number of character is: 5

The reverse string is : aidnI