Morgan Stanley interview question

A few questions on basic command-line syntax in Unix shells: 1. How would you log output and error messages from a command to a file? 2. How would you run the same command on every file in a directory? 3. How would you find the PID of a named process (say if you wanted to kill it)?

Interview Answers

Anonymous

28 Feb 2012

1. command >file 2>&1 2. cd dir; for i in *; do command; done 3. ps | grep processname or ps -C processname

8

Anonymous

30 Apr 2013

#3 - to find the PID Or simply use: pidof

Anonymous

30 Apr 2013

Opps!! there is typo; it should be: pidof

Anonymous

3 Aug 2020

1. ./sample.sh &> output.txt :- to capture output and error message suppose sample.sh have below script: #!/bin/bash cat file1 cp file2 file3 lets assume there's no file2 exists in the current directory, returns an error message 2. from any current directory run the below line by line. for i in `find -maxdepth 1 -type f` do cat $i >> output.txt done in for loop i stores all the files (type f means normal file type) in a current directory (maxdepth 1), suppose you want to consider all subdirectory files remove this part. then redirecting all the files to output.txt 3. ps -ef | grep -i "*process name* ps displays running processes, -e utility here displays all the processing shell processes without ignoring any process, f gives the full text/all info of processes. if you are not sure on the exact process name give * as above by giving part of the process name that you can remember

Anonymous

4 Mar 2012

#3 I disagree, more like ps aux |awk '$0 ~ /ProcessName/ && $0 !~ /awk/ {print $2}' If you want the PID

Anonymous

25 Aug 2012

#3 To find the PID: pgrep -x