Question

In: Computer Science

Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in...


Write a shell program named HELLO2(this should be done in linux, using bash)
Consider files in the current directory whose names end in 't' or 'z'.
For such files only, your program should list certain LINES of the file(s).
These lines are those that have at least 4 x's somewhere in the line.
Note that the x's may be upper- or lower-case, and may be separated by
other characters; so the following 3 lines DO match:
XXxX and more things
ABC xyzxXd efX abc xSx
xxxx
Your program should preface each line by the filename and a colon.
Your program should not print any error messages, even no such files exist.

Solutions

Expert Solution

==>Program

#! /bin/sh

option1='t'

option2='z'

required_count=4

for i in $PWD/*

do

file_name=$(basename $i)

file_name=$(file_name%%.*}

last_letter=${file_name: -1}

if [ $last_letter = $option1 -o $last_letter = $option2 ]

then

while read line; do

count_x = $(grep - o "[x|X]" <<< "$line" | wc -l)

if [ $count_x -ge $required_count ]

then

echo "$file_name : $line"

fi

done < $i

fi

done

------------------------------------------

In this program first we get the current directory by $PWD then we loop through each file present in current directory ...we first extracted the file name with extension and then extracted only the filename without extension ...and then the last letter of file name...

Then checked the condition as per given in question ... if its true then we loop through each line of file and count occurence of letter x + X by using grep command and then if its greater than 3 we output it as desired in question..


Related Solutions

write a bash shell program named L1 as follows . L1 expects exactly 2 command line...
write a bash shell program named L1 as follows . L1 expects exactly 2 command line arguments. expects $1 to contain only digits. expects $2 to have exactly 4 characters (can be any character). if NOT exactly 2 command line arguments, echo "need 2 args " and exit. otherwise, if $1 and/or $2 have incorrect value(s) (as above), echo "bad argX", where X is the first incorrect argument, and exit. otherwise, echo "good ". you can use echo,grep, pipe to...
I am attempting to write a script using bash on Linux. My program must save the...
I am attempting to write a script using bash on Linux. My program must save the long list format of the parent directory to a text file. Then, it must print how many items are in this parent directory. Then, it must sort the file in reverse order and save it to a different text file. I understand that the parent directory is accessed using cd .., and I understand that a file can be written to by echoing and...
Linux Sign into your lab account Write a bash shell script that does the following: Print...
Linux Sign into your lab account Write a bash shell script that does the following: Print out a friendly welcome message Ask the user for the name of their favorite animal Grep that animal from a text file noises.txt Tell the user what that animal says (i.e. what noise does it make) If the animal does not exist ask the user what noise the animal makes Store that new information in noises.txt
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
This program is a simple version of the linux shell using execvp by forking using C...
This program is a simple version of the linux shell using execvp by forking using C Currently this program of the shell assumes user enters no spaces before and no extra spaces between strings. Using exec, an executable binary file (eg: a.out) can be converted into a process. An example of using exec is implementing a shell program or a command interpreter. A shell program takes user commands and executes them. int execvp(const char *file, char *const argv[]); Same as...
Using C Write a program that will serve as a simple shell. This shell will execute...
Using C Write a program that will serve as a simple shell. This shell will execute an infinite for loop. In each iteration of the loop, the user will be presented with a prompt. When the user enters a command, the shell will tokenize the command, create a child process to execute it and wait for the child process to be over. If the user enters an invalid command, the shell should recognize the situation and show a meaningful message....
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT