Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment 1. Hint: Commands to study to answer this question: grep, wc, echo, p

ID: 3904898 • Letter: A

Question

Assignment 1. Hint: Commands to study to answer this question: grep, wc, echo, pipe ( | ), ps, process states, and man pages for ps optionsNOTE: Use man command before you start answering this question and before issuing script command to find the options to use with the ps command to find the answers in this question as well as to find the characters representing the various states of a process.

1. Issue appropriate command to display your processes, that is, processes for which you are the owner

2. Display information about your processes using long option

3. Examine the output of the previous command and with echo command, display the name (command name) and the state character for your shell process and the meaning of the state character.  You have to look at the man pages for the ps command to see the meaning of the process state character before doing this question when the script is not in effect. Never issue man command when script command is in effect.

4. Using echo command, answer the process id (pid) of the shell process and the parent pid of your shell process

5. Display all the processes in the system using an appropriate option other than long option

Pipe takes the standard output of the command preceding it and feeds to standard input of the command following it. The pipe is indicated by | character between the commands as

Command1 | Command2

The grep command can be used to search for a string in one or more files. When the line with the string is found, it is displayed.

The wc command can be used to find the number of lines, number of words and number of character in a file or the input supplied to it.

Using grep and pipe do the following:

6. Issue appropriate command to display all the processes in the system and pipe the output to the grep command to display the line containing the initprocess.

7. Using the echo command answer the process ID (pid) of the init process.

Explanation / Answer

solution:-

Below given the description of how this step should be followed in linux scripts.

1) ps -ef | egrep -i user_name

here ps -ef command is used to see the process and the output of ps -ef is passed to the egrep command where we want to look for processes run by a particular user. “-i” is for case insensitive.

2) ps auxl | egrep -i user_name

Here ps aux is used to see all processes {a for all, u for user, x for executed and l for long option} then the output is passed to the egrep command for getting the processes run by a particular use.

3) Pid=ps auxl | egrep -i user_name

Command= $(echo `history |tail -n2 |head -n1` | sed 's/[0-9]* //')

echo $Pid

echo $Command

  here the output of previous command is stored to Pid and in the Command variable we are actually finding out the last executed command so from the output of history command we are using tail to get the last two command and head is used to get the 2nd last command. Using sed if any integer is there we are removing it.

echo $pid

here using ps -ef command and using awk command we are only sorting the PID and PPID of a shell process and it is storing in a variable pid. After that using echo command we are calling that variable to get the output. This is how it is written in scripts.

5) ps aux

Using ps aux we can get all processes run by all users.

6) ps -ef | grep -w “init”

Here the using ps -ef we get the processes and using pipe we redirect the output to grep command. Using “w” to check only the init named processes running on the system.

7) Pid =` ps -ef | grep -w "init" | awk {'print $2'}`

echo $Pid

Here using the previous output, we are sending that output to the awk command to get only the PID of the init process.