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

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...
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: ?...
Writing your own shell in Linux with execvp Only code void cmdToArray() Program should be able...
Writing your own shell in Linux with execvp Only code void cmdToArray() Program should be able to execute ls -l. #include <stdio.h> #include <string,h>// strcmp #include <stdlib.h>// exit #include <unistd.h>// fork, exec #include <sys/wait.h>// wait #define MAXARGS 20 #define ARGLEN 256 void execute(char *cmd, char *arglist[]); void cmdToArray(char *cmd, char *arglist); int main(void) { int pid; char *arglist[MAXARGS+1]; // array of pointers char cmd[ARGLEN]; // read stuff here while (1) { printf("cmd> "); fgets(cmd, ARGLEN, stdin); // remove newline from...
Design a shell program to remove all the shell programming files ending with sh on your...
Design a shell program to remove all the shell programming files ending with sh on your home directory when a SIGINT signal is received.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT