Posts

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