Thursday 9 August 2012


Shell Script Interview Question and Answers part 3 ?


echo $SHELL



./process-name &


$1, $2 and so on. $0 is your script name.


if then … fi


-eq, -ne, -lt, -le, -gt, -ge


-s filename tells you if the file is not empty, -f file...


! tests for logical not, -a tests for logical and, and ...


$#


if then elif fi


for in do done



while do done



case in ) ;; ) ;; esac


read


function-name()


kill 0


ps -ag


kill pid


date


who


pwd


rm



rm -rf



whoami


mail somebody@interviewduniya.com -s ‘Your subject’ -c ...


wc


grep string filename


grep string *


grep -r string *


They are process IDs given to processes. A PID can vary...


ps


#################################################################################

1. How to print nth column of a pattern/file without using awk,cut commands?


awk {print $n} < filename
where n is the field number



2.How to extract the second row of a text-file?

We can use either 
head -2 file.dat | tail -1 or 
cat file.dat | sed -n 2p > output.dat

sed -n 2p

3. Write a shell script that accept 7 numbers in loop?

Write a shell script that accept 7 numbers in loop,then make a sum of this numbers. Then count the numbers that are greater than 250 ?

Code
  1. #!/bin/bash
  2. total=0
  3. c=0
  4. for a in {1..3};
  5.   do
  6.         echo -"Write a number : "
  7.         read i
  8.         if [ $i -ge 250 ] ; then let "c=c+1"
  9.         fi
  10.         let "total = i + total"
  11.   done
  12. echo "total = $total"
  13. echo " > 250 = $c "













4. How would you print just the 25th line in a file ?
send -n 25 p
Code
  1. head -n25 $filename |tail -n1






   cat filename.txt | cut -c 10-20
   sed -n 25p file_name
   cut -c 10-20 filename


small shell script that adds an extension ".New" to all the files in a directory

ls -lrt | awk {priint "mv "$9" "%9".New"} | sh

Code
  1. for i in *;
  2. do mv $i $i.New;
  3. done




No comments:

Post a Comment