Posts

Showing posts from September, 2022

Write a script to replace 20% lines in a C file randomly and replace it with the pattern

Image
 <<doc Name:Omprakash A Timashetti  Date:24/08/2022 Description:Write a script to replace 20% lines in a C file randomly and replace it with the pattern Sample Execution: 1. ./replace_DEL.sh main.c Before replacing #incude <stdio.h> int main() {          printf(“Hello world\n”); } After replacing #incude <stdio.h> int main() { <-----------Deleted------------> } 2. ./replace_DEL.sh main1.c Error : No such a file. 3. ./replace_DEL.sh main2.c Error : main2.c is empty file. So can’t replace the string. 4. ./replace_DEL.sh Error : Please pass the file name through command line. doc #!/bin/bash if [ $# -ne 0 ]                                               #check for command line argument passed or not then     if [ -f $1 ]                                              #check for file present or not     then         if [ -s $1 ]                                          #Check for content of file           then             echo "Before replacing"               

For each directory in the $PATH, display the number of executable files in that directory

Image
  <<doc Name:Omprakash A Timashetti Date:24/08/2022 Description:For each directory in the $PATH, display the number of executable files in that directory Sample execution: 1. ./executable_path.sh Current dir: /usr/local/sbin current count: 0 Current dir: /usr/local/bin current count: 0 Current dir: /usr/sbin current count: 205 Current dir: /usr/bin current count: 1889 Current dir: /sbin current count: 187 Current dir: /bin current count: 159 Current dir: /usr/games current count: 5 Current dir: /usr/local/games current count: 0 Total – 2445 doc #!/bin/bash Array=(`echo $PATH | tr ":" " " `)        #Get all the directory n Array total=0                                   #initialize total =0 for i in ${Array[@]}                        #using loop to get one by one directory do    if [ -d $i ]                          #check for directory     then         count=0                           #initialize count=0         echo "Current dir:$i"            #Pri

Count the number of users with user IDs between given range.

Image
 #!/bin/bash <<doc Name : Omprakash A Timashetti Date : 21/08/2022 Description :Count the number of users with user IDs between given range. Sample Input1: ./user_ids.sh Sample Ouput1:Total count of user ID between 500 to 10000 is: 2 Sample Input2: ./user_ids.sh 0 100 Sample Output2:Total count of user ID between 0 to 100 is : 3 Sample Input3:./user_ids.sh 100 Sample Output3:Error : Please pass 2 arguments through CL.                Usage : ./user_ids.sh 100 200 Sample Input4:./user_ids.sh 200 100 Sample Output4:Error : Invalid range. Please enter the valid range through CL. doc #Count the number of of users  with user id between given range.  Array=($(cut -d ":" -f3 /etc/passwd))                                #Store userId in Array count=0                                                              #Initialize count=0 if [ $# -eq 0 ]                                                      #If no Command line argument is passed then                                       

Script to print contents of a directory without ls command

Image
  #!/bin/bash <<doc Name :Omprakash A Timashetti Date : 19/08/2022 Description :Script to print contents of a directory without ls command Sample Input1 :./output_ls.sh Sample Output2: Assignments Classwork Sample Input2:./ouput_ls.sh ~ ~/ECEP Sample Output:/home/user:               Downloads Documents Desktop Music Pictures Public Videos               ECEP               /home/user/ECEP:               Linux_Systems Advnc_C Linux_Internals Data_Structures MC Sample Input3:./output_ls.sh test Sample Output3:output_ls.sh: Cannot access ‘Test’ : No such a file or directory. doc #Script to print contents of a directory without ls command if [ $# -eq 0 ]                         #Check the command line argument then     echo *                              #Print the content of current working directory else     dir=($@)                            #Directory Name to be store in array     for i in ${dir[@]}     do                                  #to get one by one directory through for l

Write a script to determine whether a given file system or mount point is mounted

Image
  <<doc Name:Omprakash A Timashetti Date:24/08/2022 Description:Write a script to determine whether a given file system or mount point is mounted Sample Execution: 1. ./mounted_fs.sh /dev/sda2 File-system /dev/sda2 is mounted on / and it is having 98% used space with 3298220 KB free. 2. ./mounted_fs.sh /dev /dev is not mounted. 3. ./mounted_fs.sh Error : Please pass the name of the file-system through command line. doc #!/bin/bash if [ $# -ne 0 ] then FileSys=(`df | cut -d " " -f1`)                      #To get File System Available=(`df | tr -s " " | cut -d " " -f4`)        #To get Available space Use=(`df | tr -s " " | cut -d " " -f5`)              #To get Usage mounted=(`df | tr -s " " | cut -d " " -f6`)         #To get where file system mounted count=1                                        #initialize count=1 to know file mounted or not for i in `seq 1 $((${#FileSys[@]}-1))`             do     if [ $1  = $

Use a recursive function to print each argument passed to the function

Image
  <<doc Name:Omprakash A Timashetti Date:23/08/2022 Description:Use a recursive function to print each argument passed to the function Sample Input1:./recursion.sh How are you? I am fine Sample Output1:How                are                you?                I                am                fine Sample Input2:./recursion.sh Sampele Output2:Error : Pass the arguments through command line. doc #!/bin/bash if [ $# -ne 0 ]                         #check for the command line Arguments then function display()                      #Function defination   {     Array=($@)     echo $1                            #print first argument in      Array=(${Array[@]:1})              #Store it in Array     if [ $# -eq 0 ]                   #Checking for end of argument     then         return                         #Exit from loop     fi     display ${Array[@]}              #function call for recursive operation } display $@                           #function call else     echo "Error : P

Shell script to convert string lower to upper and upper to lower

Image
 #!/bin/bash <<doc Name : Omprakash A Timashetti Date : 18/08/2022 Description : Shell script to convert string lower to upper and upper to lower Sample Input :./upper_lower.sh file.txt               1 – Lower to upper               2 – Upper to lower               Please select option : 1 Sample Output:WHAT IS OS?               WHAT ARE THE DIFFERENT OS?               WHEN IS OS USED?               WHAT IS PARTITION AND ITS USE?               HOW MANY PARTITIONS CAN BE DONE? Sample Input :./upper_lower.sh file.txt               1 – Lower to Upper               2 – Upper to Lower               Please select option : 2 Sample Output :what is os?               what are the different os?               when is os used?               what is partition and its use?               how many partitions can be done? Sample Input:./upper_lower.sh file1.txt Sample Output:Error: No contents inside the file. Sample Input:./upper_lower.sh Sample Output: Error : Please pass the file name through

Write script called say_hello, which will print greetings based on time

Image
  <<doc Name : Omprakash A Timashetti Date:24/08/2022 Description:Write script called say_hello, which will print greetings based on time Sample Execution: 1. When we start shell (whenever you opening new tab or terminal) Good Morning user, Have nice day! This is Thursday 08 in June of 2017 (10:44:10 AM) doc #Write script called say_hello, which will print greetings based on time #!/bin/bash Hour=`date | cut -d " " -f4 | cut -d ":" -f1`      #Get Hour Day=`date | cut -d " " -f1`                        #To get only Day Date=`date | cut -d " " -f3`                      #To get Date Month=`date | cut -d " " -f2`                     #To get Month Year=`date | cut -d " " -f6`                      #To Get Year FullHour=`date | cut -d " " -f4`                  #To get fulltime   Meridian=`date +%p`                               #To get AM/PM if [ $Hour -gt 5 -a $Hour -le 12 ] then                                     

Display the longest and shortest user-names on the system

Image
 #!/bin/bash <<doc Name : Omprakash A Timashetti Date :20/08/2022 Description :Display the longest and shortest user-names on the system Sample Input :./largestst_username.sh Sample Output :The Longest Name is: speech-dispatcher                The Shortest Name is:lp doc Array=($(cut -d ":" -f1 /etc/passwd)) smallest=${Array[0]}                                            #initialize small with first user name largest=${Array[0]}                                             #initialize large with first user name for i in `seq 1  $((${#Array[@]}-1))`                           #looping number of user do     if [ ${#largest} -lt  ${#Array[$i]} ]                       #condition to Check for largest user name       then            largest=${Array[$i]}          fi     if [ ${#smallest} -gt  ${#Array[$i]} ]                      #Condition to check fo the smallest user name       then            smallest=${Array[$i]}    fi done echo "The Longest Name is: $largest"     

Given album name and corresponding directory, this scripts renames the jpg files with new name passed through command line

Image
 #!/bin/bash <<doc Name : Omprakash A Timashetti Date : 18/08/2022 Description :Given album name and corresponding directory, this scripts renames the jpg files with new name passed through command line Sample Execution: Before executing the script $ ls DSN001.jpg DSN002.jpg DSN003.jpg DSN004.jpg DSN005.jpg 1) ./rename_album.sh day_out All .jpg files in current directory is renamed as day_out001.jpg day_out002.jpg day_out003.jpg day_out005.jpg day_out004.jpg 2) ./rename_album.sh Error : Please pass the prefix name through command line. doc echo "Before executing the script" ls                               #Get the directory content before executing the script if [ $# -eq 1 ]                              #Check for command line argument then     array=(`ls *.jpg`)                       #Get only .jpg file and store it in array     for i in ${array[@]}                         do          num=`echo $i | tr -cd [:digit:]`    #get if any digit in filename         mv $i $1$n

Write script to print contents of file from given line number to next given number of lines

Image
  #!/bin/bash <<doc Name : Omprakash A Timashetti Date : 16/08/2022 Description :Write script to print contents of file from given line number to next given number of lines. Sample Input :./print_lines.sh 5 3 myfile.txt Sample Output :line number 5                line number 6                line number 7 Sample Input :./print_lines.sh myfile.txt Sample Output :Error: arguments missing!                Usage: ./file_filter.sh start_line upto_line filename                For eg. ./file_filter.sh 5 5 <file> Sample Input :./print_lines.sh 5 10 myfile.txt Sample Output :Error: data.txt is having only 10 lines. file should have atleast 14 lines doc if [ $# -eq 3 ]                      #Check for the command line Arguments then total=$(($1+$2-1)) lines=$(cat $3 | wc -l)              #Get the number of lines ina file if [ $lines -lt $total ]             #Comppare number of lines in file and required number of lines   then     echo "Error: $3 is having only $lines lines. file s

Shell script to convert string lower to upper and upper to lower

Image
 #!/bin/bash <<doc Name : Omprakash A Timashetti Date : 18/08/2022 Description : Shell script to convert string lower to upper and upper to lower Sample Input :./upper_lower.sh file.txt               1 – Lower to upper               2 – Upper to lower               Please select option : 1 Sample Output:WHAT IS OS?               WHAT ARE THE DIFFERENT OS?               WHEN IS OS USED?               WHAT IS PARTITION AND ITS USE?               HOW MANY PARTITIONS CAN BE DONE? Sample Input :./upper_lower.sh file.txt               1 – Lower to Upper               2 – Upper to Lower               Please select option : 2 Sample Output :what is os?               what are the different os?               when is os used?               what is partition and its use?               how many partitions can be done? Sample Input:./upper_lower.sh file1.txt Sample Output:Error: No contents inside the file. Sample Input:./upper_lower.sh Sample Output: Error : Please pass the file name through

Write a script to print system information using commands

Image
  #!/bin/bash <<doc Name:Omprakash A Timashetti Date:23/08/2022 Description:Write a script to print system information using commands Sample Execution: ./system_info.sh 1. Currently logged users 2. Your shell directory 3. Home directory 4. OS name & version 5. Current working directory 6. Number of users logged in 7. Show all available shells in your system 8. Hard disk information 9. CPU information. 10. Memory Informations 11. File system information. 12. Currently running process. Enter the choice : 2 Your shell directory is /bin/bash doc option=y                            #For first time initialize option to y=yes to get first time menu while [ $option = y ] do echo "1. Currently logged users"     #Dispaly menu echo "2. Your shell directory" echo "3. Home directory" echo "4. OS name & version" echo "5. Current working directory" echo "6. Number of users logged in" echo "7. Show all available shells in y

Write a script to sort a given number in ascending or descending order

Image
 #!/bin/bash <<doc Name:Omprakash A Timashetti Date:23/08/2022 Description :Write a script to sort a given number in ascending or descending order  Sample Input1:./sorting.sh -a 5 4 6 2 3 8 9 7 1 Sample Output :Ascending order of array is 1 2 3 4 5 6 7 8 9 Sample Input2 :./sorting.sh -d 5 4 6 2 3 8 9 7 1 Sample Output2 :Descending order of array is 9 8 6 5 4 3 2 1 Sample Input3:./sorting.sh 5 4 6 2 3 8 9 7 1 Sample Output3 :Error : Please pass the choice.                 Usage : ./sorting -a/-d 4 23 5 6 3 Sample Input4: ./sorting.sh Sample Output:Error : Please pass the argument through command line.               Usage : ./sorting -a/-d 4 23 5 6 3 Sample Input5:./sorting.sh -d 5 4 6 2 -3 8 -9 7 1 Sample Output : Descending order of array is 8 7 6 5 4 2 1 -3 -9 doc if [ $# -ne 0 ]                                        #check for command line argument passed or not then Array=($@)                                             #Store the command line argument  in an array option=$1 

Write a script to print chess board

Image
  #!/bin/bash <<doc Name : Omprakash A Timashetti Date : 12/08/2022 Description : Write a script to print chess board Sample Input : .. Sample Output : Chess board to be printing on output. doc #Script to print chess board for row in `seq 1 8`                 #Rows of chesss board do for col in `seq 1 8`             #columns of chess board do rem=$(((row+col)%2))            #To get like chess board pattern we need check row equal to column if [ $rem -eq 0 ] then echo -n -e "\e[40m "    #Black box in chess board else echo -n -e "\e[47m "    #white box in Chess Board fi done echo -n -e "\e[0m "            #To make it normal     echo  done

Write a script to print the length of each and every string using arrays

Image
 #!/bin/bash <<doc Name : Omprakash A Timashetti Date : 16/08/2022 Description : Write a script to print the length of each and every string using arrays Sample Input :./string_length.sh hello hai how are you? Sample Output :Length of string (hello) - 5                Length of string (hai) - 3                Length of string (how) - 3                Length of string (are) - 3                Length of string (you?) - 4 Sample Input  :./string_length.sh Sample Output : Error : Please pass the arguments through command-line. doc if [ $# -ne 0 ]                                                  #Check for command line Arguments then     Array=( $@ )                                                 #Assigning Value to Array      for i in `seq 0 $((${#Array[*]}-1))`                         #Get number of Elements     do         echo "Length of string (${Array[$i]}) - ${#Array[$i]}"  #Print the elements and Length of element                   done else     echo "Error : Pl

Write a script to perform arithmetic operation on digits of a given number

Image
 #!/bin/bash <<doc Name : Omprakash A Timashetti Date : 17/08/2022 Description : Write a script to perform arithmetic operation on digits of a given number Sample Input :./operator_dependent.sh 1234+ Sample Output:Sum of digits = 10  Sample Input:./operator_dependent.sh 8312- Sample Output :Subtraction of digits = 2 Sample Input :./operator_dependent.sh 5487 Sample Output : Error: Operator missing or invalid operator, please pass operator as last digit (+,-,x,/ Sample Output :./operator_dependent.sh 1111x Sample Output :Multiplication of digits = 1 Sample Input :./operator_dependent.sh 12/ Sample Output:Division of digits = .50 Sample Input : ./operator_dependent.sh Sample Output : Error : Please pass the arguments through CL.                 Usage : ./operator_dependent.sh 2345+ doc #Script to perform arithmatic operation on digits of a iven number if [ $# -eq 1 ]                        #check for command line arguments then string=$1                              #istring of dig

Write a script to delete empty lines from a file

Image
If file contains OUT PUT will be     #!/bin/bash <<doc  Name : Omprakash A Timashetti  Date : 17/08/2022 Description: Write a script to delete empty lines from a file Sample Input: ./delete_empty_lines.sh file.txt Sample Output: Empty lines are deleted                Hello                How                are                you? Sample Input :./delete_empty_lines.sh Sample Output :Error: Please pass the file name through command line. doc #Script to delete Empty line from file if [ $# -eq  1 ] then      sed -i '/^[[:blank:]]*$/d' $1                #Command to delete empty lines,blank lines from file     cat $1 else     echo "Error: Please pass the file name through command linei." #if no file passed it will givean Error fi

Write a script for arithmetic calculator using command line arguments

Image
 #!/bin/bash <<doc Name : Omprakash A Timashetti Date : 12/08/2022 Description : Write a script for arithmetic calculator using command line arguments Sample Input :./arithmatic_calc.sh 25 + 41 Sample Output : 25 + 41 = 66 Sample Input :./arithmatic_calc.sh 10 x 5  Sample Output : 10 x 5 = 50 Sample Input :./arithmatic_calc.sh 25 / 5 Sample Output : 25 / 5 = 5.00 Sample Input :./arithmatic_calc.sh 10.2 - 5.6 Sample Output : 10.2 - 5.6 = 4.6 Sample Input :./arithmatic_calc.sh  Sample Output :Error : Please pass the arguments through command line.                Usage:./arithmatic_calc.sh 2.3 + 6.7 Sample Input :./arithmatic_calc.sh 25 Sample Output : Error:Please pass 3 arguments.                 Usage:./arithmatic_calc.sh 2.3 + 6.7 doc if [ $# -eq 0 ]     #check for the command line arguments                               then      echo "Error : Please pass the arguments through command line."     echo "Usage:./arithmatic_calc.sh 2.3 + 6.7"   elif [ $# -eq 3 ]

Read 'n' and generate a pattern given below(number increasing from Top to bottom)

Image
  #!/bin/bash <<doc Name        :Omprakash A Timashetti. Date        :11/08/2022. Description :Read 'n' and generate a pattern given below(number increment from top to bottom) Sample input:Enter the number: 4 Sample Output :1                2 3                4 5 6                 7 8 9 10  doc read -p "Enter the number :" number  #Number of element i=1                                  #initialize i to get pattern for row in `seq $number`             #number of row to be printed do     for col in `seq $row`            #Number of column to be printed         do         echo -n "$i "                #Required value for pattern        i=$(($i+1))     done   echo done

Read 'n' and generate a pattern given below(number increment from left to right)

Image
 #!/bin/bash <<doc Description  : Read 'n' and generate a pattern given below(number increment from left to right) Sample input : Enter the number: 4 Sample Output:1               1 2               1 2 3                1 2 3 4  doc read -p "Enter the number :" number  #Number of element for row in `seq $number`             #number of row to be printed do     for col in `seq $row`          #Number of column to be printed         do         echo -n "$col "          #Required value for pattern          done   echo done