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

Feb 28, 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

Apr 30, 2013

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

Anonymous

Apr 30, 2013

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

Anonymous

Aug 3, 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

Mar 4, 2012

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

Anonymous

Aug 25, 2012

#3 To find the PID: pgrep -x