You are on page 1of 11

1.

Give a shell pipeline that will print the name of the file containing the least lines in the current directory. Aim: To Give a shell pipeline that will print the name of the file containing the least lines in the current directory. Pipeline: wc -l * | sort -n | head -1 2. UNIX shell scripts use the # to indicate the start of a comment. Anything that follows a # till the end of the line is ignored by the interpreter. Using grep and cut, give a shell pipeline that will extract just the comment lines (mi nus the leading #) from a shell file, program.bash and put it in a file comments.txt. Aim: To give a shell pipeline that will extract just the comment lines Pipeline: cat program.bash | grep "#" | cut -c 2- - > comments.txt 3. Generate a word frequency list for wonderland.txt. Hint: use grep, tr, sort, uniq (or anything else that you want) Aim: To Generate a word frequency list for wonderland.txt. Pipeline: cat wonderland.txt |tr ' ' '\n' |tr -d [:punct:] | sort | uniq -c | sort -n 4.Print the middle line of the file wonderland.txt. Aim: To print the middle line of the file wonderland.txt. Scipt: #!/bin/bash number= echo `wc -l $1 | cut -f 1 -d " "`/2 | bc cat $1 | head -n $number | tail -n 1 5. Write a shell script 'firstline.sh' that lists all the '.txt' files present in the current directory, and writes the first line of all those files into a file called first_line_file. Aim: To list all the '.txt' files present in the current directory, and write the first line of all those files into a file called first_line_file. Script: #!/bin/bash for i in `ls *.txt` do echo $i temp=echo `cat $i | head -n 1` echo $temp > $1 tempo='\n' echo $tempo > $1 done

6. Write a shell script called 'file_count.sh' which counts the number of files in the directory taken as a command line argument. It should not count the directories and the files inside the sub-directories.(i.e. It should not count recursively) Aim: To count the number of files in the directory. Script: count=0 for i in `ls $1` do echo $i if test -f $i then count=`expr $count + 1` fi done echo $count 1. Write a shell script to remove all executable files from a directory, when a directory is given as argument. Aim:to remove all executable files from a directory, when a directory is given as argument. Code: #!/bin/bash/ if test -d $1 then echo "valid directory" for i in `ls $1` do if test -x $i then rm $i fi done fi 2. Generate frequency list of all the commands you have used, and show the top 5 commands along with their count Aim: to print frequency of all commands used Code: history | cut -d " " -f 4 | sort | uniq -dc Aim: to print frequency of top 5 commands Code: history | cut -d " " -f 4 | sort | uniq -dc | sort -rn | head -n 5

3. Write a bash script that takes 2 or more arguments, i)All arguments are filenames ii)If fewer than two arguments are given, print an error message iii)If the files do not exist, print error message iv)Otherwise concatenate files Aim: concatenate files if arguments given are filenames and number of arguments greater than or equal to two. And also to print error message if file does not exist. Code: #!/bin/bash if test $# -lt 2 then echo "no of arguments are less than 2" exit else touch temp for i in $@ do if test -e $i then if test -f $i then cat $i temp > tempo cp tempo temp rm tempo fi else echo "$i does not exist" fi done fi 4. Write a shell script 'swap.sh' which takes two file names as command line arguments, and checks if the files exist in the current directory. If the files exist, the shell script has to swap the contents of the files, and displays the files' contents. Aim: To swap two files and display the contents Code: #!/bin/bash if [ $# -ne 2 ] then echo "improper number of arguments " exit 0 fi if test -f $1

then if test -f $2 then echo "before swapping" echo "file 1" cat $1 echo "\nfile 2" cat $2 cat $1 > temp cat $2 > $1 cat temp > $2 echo "\nafter swapping" echo "\nfile 1" cat $1 echo "\nfile 2\n" cat $2 fi fi Output: before swapping file 1 This is file 1 file 2 This is file 2 after swapping file 1 This is file 2 file 2 This is file 1 5. How to write shell script that will add two nos, which are supplied as command line argument, and if this two nos are not given show error and its usage . Aim:To add two numbers. Code: #!/bin/bash if [ $# -ne 2 ] then echo "improper number of arguments" exit 0 fi name=$[ $1 + $2 ] echo " sum is $name " 6. Write Script to find out biggest number from given three nos. Nos are supplies as command line argument. Print error if sufficient arguments

are not supplied. Aim: To find the biggest number among three given numbers. Code: #!/bin/bash if [ $# -ne 3 ] then echo "improper number of arguments" exit 0 fi if [ $1 -gt $2 ] then if [ $1 -gt $3 ] then echo "$1" fi else if [ $2 -gt $3 ] then echo "$2" else echo "$3" fi fi 7. Write script to print nos as 5,4,3,2,1 using while loop. Aim: to print nos as 5,4,3,2,1 using while loop Code: #!bin/bash i=5 j=0 while [ $i -ne $j ] do echo $i i=$[ $i - 1] done 8. Write Script, using case statement to perform basic math operation as follows + addition - subtraction * multiplication / division The name of script must be 'q4' which works as follows $ ./q4 20 / 3, Also check for sufficient command line arguments Aim:To perform basic math operation

Code: #!bin/bash case $2 in +) echo `echo $1`+$3 | bc ;; -) echo `echo $1`-$3 | bc ;; /) echo `echo $1`/$3 | bc ;; *) echo `echo $1`*$3 | bc ;; esac 9. A shell script is a text file containing shell commands. It must start with a line like #!/bin/bash, which names the shell interpreter to use. Write a shell script, guess.sh that implements a simple number guessing game. When run, the shell scriptprompts the user to guess a random number between 0 and 100. If the user guesses incorrectly, the script prompts the user Guess higher or Guess lower as appropriate and allows the user to guess again. If the user guesses correctly,the script terminates printing out how many guesses the user needed. For example: nitt$ ./guess.sh Guess my secret number (0-100): 101 Thats not between 0 and 100 Guess my secret number (0-100): 80 Guess higher Guess my secret number (0-100): 85 Guess lower Guess my secret number (0-100): 82 Right! You guessed my secret in 3 guesses. Aim: To implement a guess game Code: #!/bin/bash temp=$[ RANDOM%100 ] count=0 read n while [ $n -ne $temp ] do if [ $n -lt $temp ] then echo "Guess higher" count=$[ $count + 1 ] read n fi if [ $n -gt $temp ] then echo "Guess lower" count=$[ $count + 1]

read n fi done if [ $n -eq $temp ] then echo " you have tried $count times" fi PYTHON PROGRAMS 5. Write a program that displays all three digit numbers that are equal to the sum of the cubes of their digits. That is, print numbers that have the property These are called armstrong numbers. Aim: To print armstrong numbers Code: num = 100 while num<=999: temp = num value = 0 while temp!=0: mod = temp%10 temp = temp/10 value = value + mod * mod * mod if value==num: print num num = num + 1 7. Kaprekar's constant 1. Take a four digit numberwith at least two digits different. 2. Arrange the digits in ascending and descending order, giving A and D respectively. 3. Leave leading zeros in A! 4. Subtract A from D. 5. With the result, repeat from step 2. Aim: To print the number of iterations requires to calculate kaprekar's constant from the given input Code: print "enter the value" ip = raw_input() num = int(ip) times=0 rem=[0,0,0,0] numcopy=0 while numcopy!=num: i=0

numcopy=num while num>0: rem[i]=num%10 num=num/10 i=i+1 i=0 while i < 3: j=i+1 while j<4: if rem[i]>rem[j]: temp=rem[i] rem[i]=rem[j] rem[j]=temp j=j+1 i=i+1 #print rem[:] i=0 numasc = rem[0]*1000+rem[1]*100+rem[2]*10+rem[3] numdesc=rem[3]*1000+rem[2]*100+rem[1]*10+rem[0] num=numdesc-numasc print num times = times + 1 print times 11.Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares. For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square). Aim: To generate a list of all four digit numbers that have all their digits even and are perfect squares. Code: num = 1000 numrange = 9999 for num in range numrange : temp = num k=0 while temp!=0: mod = temp%10 temp = temp/10 if mod%2==1: k=1 break if k==0: i = 31 irange = 100 for i in range irange: if i*i=num:

print num 12.The aliquot of a number is defined as: the sum of the proper of the number. For example, the aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16. Write a function that returns the aliquot number of a given number. Aim: To print aliquot number of a given number. Code: print "enter num\n" ip = raw_input() num = int(ip) i=1 sum=0 while i<num: if num%i==0: sum=sum+i i = i+1 print sum 13. A pair of numbers (a, b) is said to be amicable if the aliquot number of a is b and the aliquot number of b is a. Example: 220, 284 Write a program that prints all five digit amicable pairs. Aim: To print all five digit amicable pairs. Code: num=1 while num<=1000: i=1 sum=0 while i<num: if num%i==0: sum=sum+i i=i+1 k=1 sum1=0 while k<sum: if sum%k==0: sum1=sum1+k k=k+1 if sum1==i: print i print sum print "\n" num = num + 1

18. Write a program to display the following pyramid. The number of lines has to be a parameter obtained from the user. The pyramid must appear aligned to the left edge of the screen. Aim: To display required pyramid. Code: print "enter the number of lines" num = raw_input() nooflines = int(num) i=1 while i<=nooflines: j=1 while j<=i: print i*j, j=j+1 print "" i=i+1 Output: enter the number of lines 5 1 24 369 4 8 12 16 5 10 15 20 25 20.Given a string like, "1, 3-7, 12, 15, 18-21", produce the list [1,3,4,5,6,7,12,15,18,19,20,21] Aim: To produce the desired list Code: ip=raw_input() print ip new=ip.split(",") length=len(new) print length i=0 while i<length: new1=new[i].split("-") length1=len(new1) if length1==1: print new1[0] length1=0 else: num1=int(new1[0]) num2=int(new1[1])

while num1<=num2: print num1 num1=num1+1 i+1