GLA University, Mathura
Course: B. Tech (CSE/ IBM related courses) UNIX OS Lab/BCSC 0804
Session: 2020-21
- Command Implementation On Shell
- b. Implement the special shell scripting variables such as $$,$0,$1,$*,$@,$# etc
#!/bin/bash #Implement some Commands $0,$$,$1,$*,$# Etc. echo " File Name : $0" echo "Process Id : $$" echo " First Argument : $1" echo "Second Argument : $2" echo "Third Argument : $3" echo "Quoted values : $@" echo "Quoted values ; $*" echo "Number Of Arguments : $#" |
- SHELL PROGRAMMING
3. Shell scripts that uses simple commands:
a. Write a shell script to display current date in a particular format, number of users currently login and current month’s calendar
#!/bin/bash #Display Current Date,Calender and Number of User echo "Current Date :"; date echo "Current Month's of Calender"; cal echo "Number of User :";who -q |
b. Write a shell script to display the process name and its process id.
#!/bin/bash #Display Process Name and Process Id echo "Current Process Name: $0" echo "Current Process Id : $$" |
c. Write a shell script to take name as a input and display a greeting message to the user by checking system clock. (Ex :- Hello John Good Morning in morning time else Hello John Good Afternoon in afternoon time else Hello John Good Evening in Evening time).
#!/bin/bash #Greeting User by Present Time echo "Enter the Name Of User:" read Name Present_Time=$(date +%H) if [ $Present_Time -lt 12 ] then echo " Good Morning! : $Name " elif [ $Present_Time -gt 12 -a $Present_Time -lt 15 ] then echo " Good AfterNoon! : $Name " elif [ $Present_Time -gt 15 -a $Present_Time -lt 18 ] then echo " Good Evening! : $Name " else echo " Good Night! : $Name " fi |
#!/bin/bash #concatenate Two Files echo Enter first filename read File1 echo Enter second filename read File2 cat $File1 > third cat $File2 >> third echo After concatination of contents of entered two files echo ---------------------------------------------------- cat third | more echo ---------------------------------------------------- |
4. Decision based Shell scripts:
#!/bin/bash #Check the number is even or odd echo "Enter the Number" read Num test $(($Num % 2)) == 0 && echo "Number is Even" || echo "Number is Odd" |
#!/bin/bash #check the argument is file or dir or not if [ -f$1 ] then echo "It is a File" elif [-d$1 ] then echo "It is a directory" else echo "It is not a file or dir" fi |
c. Write a shell script to input the marks of a student in 3 subjects and find his grade.
#!/bin/bash if [ $Avg -ge 60 ] |
#!/bin/bash #print last modification time of file if [ -f$1 ] then echo "Last Modify Time : $(stat -c %y $1)" else echo "it is not a File" fi |
#!/bin/bash #Enter the file as input and if exist print the number of lines and 10th line of that file echo "Enter the file name" read file if [ -f$file ] then l=`cat $file | wc -l` ll=`cat $file | head -10 | tail -1` echo "Number of lines = $l" echo "10th line of file=$ll" else echo "File does not exist" fi |
5. Shell scripts related to loops and arrays
a. Write a shell script that print multiplication table of a given no.
#!/bin/bash #Print Multiplication Table Of given no by user echo "Enter the Number:" read num echo "Enter the Higher range of number You want to multiply" read Num echo "Multiplication Table" for ((i=1;i<=$Num;i++)) do echo "$num x $i=`expr $num \* $i`" done |
b. Write a shell script to implement a timer.
#!/bin/bash #set timer flag=1 while [ $flag -eq 1 ] do echo "Enter a Timer Limit" read t for ((i=$t;i>0;i--)) do echo "Current Time : $(date +%T)" sleep 1 wait done echo "Time Up : $(date +%T)" echo "If You Want new Timer then press 1 for Exit press 0" read flag done |
c. Write a shell script that print reverse of a given positive number.
#!/bin/bash #print the Number in reverse order echo "Enter the Number" read Num rem=0 res=0 while [ $Num -gt 0 ] do rem=$(($Num % 10)) res=`expr $res \* 10 + $rem` Num=$(($Num / 10)) done echo "Reverse No. : $res" |
#!/bin/bash #factorial Of Number echo "Enter the Number" read Num res=1 for ((i=$Num;i>0;i--)) do res=`expr $res \* $i` done echo "Factorial of Num : $res" |
#!/bin/bash #Array Operations SizeOfArr=10 count=$SizeOfArr i=0 echo "Enter the Array Value" while [ $i -lt $SizeOfArr ] do read arr[$i] i=`expr $i + 1` done max=${arr[0]} min=${arr[0]} sum=0 for i in ${arr[*]} do if [ $i -gt $max ] then max=$i fi if [ $i -lt $min ] then min=$i fi sum=`expr $sum + $i` done avg=`expr $sum / $count` echo "Maximum Value Of Arr : $max" echo "Minimum Value Of Arr : $min" echo "Total Sum Of Value : $sum" echo "Average : $avg" #printf %.2f $(echo "$avg | bc -1) this is not working |
#!/bin/bash #Identify Student is pass or Fail echo "Enter the No. of Student" read num_Std num=$num_Std i=1 count=0 while [ $i -le $num_Std ] do echo "Enter the student$i name only single char" read Std[$i] echo "Enter the Marks(0 - 100) Of English" read eng[$i] echo "Enter the marks(0 - 100) of Math" read Math[$i] echo "Enter the mark(0 - 100) of Science" read Sci[$i] i=`expr $i + 1` done echo "Student Mark's Details" for ((k=1;k<=${#Std[@]};k++)) do echo "Std ${Std[k]}" "Eng ${eng[k]}" "Mat ${Math[k]}" "Sci ${Sci[k]}" done echo "Final Result" for ((j=1;j<=$num;j++)) do if [ ${eng[$j]} -ge 50 ] then count=`expr $count + 1` fi if [ ${Math[$j]} -ge 50 ] then count=`expr $count + 1` fi if [ ${Sci[$j]} -ge 50 ] then count=`expr $count + 1` fi if [ $count == 3 ] then echo "Student $j is Pass in All" else echo "Student $j is Fail" fi count=0 done |
6. Shell scripts related to strings and pipes:
#!/bin/bash #Check the String input by user is same or Not echo "Enter the 1st String" read str1 echo "Enter the 2nd String" read str2 if [ $str1 = $str2 ] then echo "Strings Are Same" else echo "Strings Are Not Same" fi |
b. Write a shell script to input a string from the user and determine its length.
#!/bin/bash #find a length of String echo "Enter the String" read str echo "Length Of String : ${#str}" |
#!/bin/bash #find the occurence between two strings echo "Enter the 1st String" read str1 echo "Enter the 2nd String" read str2 grep -o -w "$str2" <<< "$str1" | wc -w |
#!/bin/bash #Enter a file using command line count word,char &line if [ -f$1 ] then w=`cat $1 | wc -w` c=`cat $1 | wc -c` l=`cat $1 | wc -l` echo "Number of words = $w" echo "Number of Char = $c" echo "Number of lines = $l" else echo "Invalid File" fi |
#!/bin/bash #Print the list of dir and how much space consume in sorted order du -sh */ | sort -nr |
#!/bin/bash #Count txt file in your dir and print number if [ -d$1 ];then echo "Number Of files is $(find "$1" -type f | wc -l)" else echo "Error" fi |
e. Write a shell script to create a tsv file containing name, roll no. and age of 10 students. Then use that tsv file to display only the names of the students in alphabetical order.
#!/bin/bash echo "Enter filename" read file echo "Sorted Name of Students" sort $file | awk '{print $1}' |
f. You are given a file of tab-delimited weather data (TSV). There is no header
column in this data file. The first five columns of this data are: (a) the name of the
city (b) the average monthly temperature in Jan (in Fahrenheit). (c) The average
monthly temperature in April (in Fahrenheit). (d) The average monthly temperature in
July (in Fahrenheit). (e) the average monthly temperature in October (in
Fahrenheit).You need to sort this file on the basis of average monthly temperature in
April.
#!/bin/bash echo "Enter filename" read file #To get April Column of file sorted sort -k3n $file |
- Note:
This Code is Verified by CodexRitik.If any error occurs then Comment correct code Below in comment box.
Great work buddy keep it up👏👍
ReplyDeleteThanks Man!
DeleteGreat work, but solution f is wrong, it is returning total no of files, while we need to count only .txt files
ReplyDeleteYaa You are right in that ques i have written to calculate only number of files where f denotes any types of files so you may use there .txt extension to calculate that file.
DeleteIn some solutions there is an error of bad for loop and something like this . How to resolve that ?
ReplyDeletei think this is error of compiler or may be linux version pls try to some change in that code.Thanks For Your Valuable Feedback.
Deleteo bhaie tera section wala to khus ho gya hoga.
ReplyDeleteOur money assignment help specialists are accessible 24x7 to answer every one of your questions and clear the entirety of your questions. Simply call us now and benefit the best administrations in the town. how to write a reflection paper
ReplyDeleteThanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. Class 9 Assignment
ReplyDeleteI was reading some of your content on this website and I conceive this internet site is really informative ! Keep on putting up. Assignment Answer
ReplyDeleteThank You so Much!
Delete