GeekInterview.com
Series: Subject: Topic:

Shell Scripting Interview Questions

Showing Questions 1 - 20 of 55 Questions
First | Prev | | Next | Last Page
Sort by: 
 | 

How would you print just the 25th line in a file (smallest possible script please)?

Asked By: Interview Candidate | Asked On: Jun 6th, 2005

Answered by: Manas on: Sep 16th, 2014

Head -25 |tail -1

Answered by: Dibya on: Aug 26th, 2014

• sed -n 10p
• cat -n | sed -n 10p
• sed -n 10p
• head -10 | tail -1
• sed -n 1,10p | tail -1
• cat | head -10 | tail -1
• head -10 | tail +10

Using bourne shell : if you enter a b C d e f g.......................N after the command,how will you write a programme to reverse these positional parameters?

Asked By: Nehashri | Asked On: Feb 4th, 2007

Answered by: Mayank Mishra on: Sep 15th, 2014

For((c=$#;c>0;c--))
do
eval echo -n "$$c "
done

Answered by: eric on: Sep 10th, 2014

Code
  1. for((c=$#;c>0;c--))
  2. do
  3. eval echo $$c
  4. done

How to delete a word from a file using shell scripting???

Asked By: vinay.prasad | Asked On: Jun 28th, 2007

Answered by: ajay goel on: Jul 7th, 2014

Code
  1. vi file
  2. escape
  3.  1,$ s/word//g
  4. :wq

Answered by: UncaAlby on: Nov 7th, 2008

please note that my original answer ate the quote signs, which I'm going to try to make sure get through this time:sed 's/\<the\>//g'And in case that gets eaten also, it's a reve...

Shell script

Asked By: Namataraginu | Asked On: Jan 31st, 2008

You have current directory containing set of directories which contain files.One file can reside in many directories.Write script which returns number of unique file names inall the subdirectories of a current dir.

Answered by: ajay goel on: Jun 28th, 2014

Find . -type f | sort| uniq

Answered by: nishi11 on: Feb 22nd, 2010

ls -pR | grep -v "/" | grep -v ":" | sort |uniq

Write a shell script to identify the given string is palindrome or not?

Asked By: athotaashok | Asked On: Feb 21st, 2006

Answered by: Shanthi on: Jun 26th, 2014

St1="abc"
for (( i=$len-1; i>=0; i-- ))
do
reve="$reve${st1:$i:1}"
done

Answered by: my_thoughts on: Feb 14th, 2010

Read string1string_rev=`rev string1`if [ string1 = string_rev ]then echo palindromeelse echo not a palindromefi

How would you replace the n character in a file with some xyz?

Asked By: Interview Candidate | Asked On: Jun 3rd, 2005

Answered by: Arun C N on: Jun 24th, 2014

Sed -n s/n/xyz/g filename

Answered by: ajay goel on: Jun 15th, 2014

Code
  1. ### general method for n number of file#####
  2.  
  3. for file in path/*
  4. do
  5. if [ -f $file ];
  6. then
  7. sed s/n/xyz/g $file
  8. else
  9. echo " $file is not file"
  10. fi
  11. done
  12.  

Print nth column of pattern

Asked By: poi677 | Asked On: May 1st, 2012

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

Answered by: ajay goel on: Jun 15th, 2014

Grep "pattern" filename | xargs print {$n}

Answered by: Saurabh Mishra on: Jun 3rd, 2014

Code
  1. echo "enter the line which is need to fetch"
  2. read n
  3. i=1
  4. while [$i -eq $n]
  5. do
  6. a=`cat file |cut -d " "-f$i`
  7. ((i = i+1))
  8. done
  9. echo $a
  10.  

Shell script to add file extension

Asked By: ayongying | Asked On: May 28th, 2010

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

Answered by: Nirmal Acharya on: May 11th, 2014

While [ ! -z "$1" ]
do
mv "$1" "$1.new"
done

Answered by: gururaj on: Jun 5th, 2012

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

What does $? Return?

Asked By: Interview Candidate | Asked On: Jun 2nd, 2005

Answered by: Kantha on: Sep 18th, 2013

$? :- provide exit status of last executed command In case the last command successfully executed then the value is 0 , if its failed to execute then the value non-zero. Example: ...

Answered by: saravanan on: Jul 9th, 2013

It gives exit status of the command.

Use echo $? to get exit status of the script

Hint : if you get 0 then success, 1 means failure and some outputs

Write a shell script that accept 7 numbers in loop

Asked By: angel rayan | Asked On: Jun 3rd, 2012

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 ?

Answered by: satish on: Jul 10th, 2013

"perl total=0 for a in 1 ...

Answered by: ardiansyah on: Jul 5th, 2012

Code
  1. #!/bin/bash
  2. total=0
  3. c=0
  4. for a in {1..3};
  5.   do
  6.         echo -n "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 "

UNIX shells

Asked By: super_sixes | Asked On: Sep 26th, 2008

What are the different types of shells available in UNIX?

Answered by: nitin on: Mar 2nd, 2013

There is a lot of shell available in UNIX.
Ex:- Bourne shell, Bash shell, CShell, Korn shell etc.

Answered by: santarvedi on: Oct 30th, 2012

$cat /etc/shells

How do u open a read only file in UNIX?

Asked By: Interview Candidate | Asked On: Jul 5th, 2005

Answered by: Nice Thomas on: Jan 2nd, 2013

Using vi editor. Read only files can be opened without any issues

Answered by: SaintHax on: Oct 4th, 2012

Anything that opens a file, can open a read only file: cat, more, less, awk, pg, et all. The chmod command you put does not make a file read only, it makes it read/write for the user/owner.

Want to delete the record if I find a word (dd)in the fixed length(5-7).

Asked By: bujji4you | Asked On: May 22nd, 2012

Want to delete the record if I find a word (dd)in the length(5-7). Lets think that (5-7) length is ff2. this is a fixed width file. ex f1 ff1 ff2 ff3 sds dd fd sd ss ew dd dd se o/p should be like ff1 ff2 ff3 sd ss ew

Answered by: SaintHax on: Oct 4th, 2012

Sed /dd/d $file

Answered by: Gaurav on: Oct 2nd, 2012

Sed s/dd //g file

How does windows NT supports multitasking?

Asked By: Beena | Asked On: Sep 19th, 2005

Answered by: Sandhya.Kishan on: Jul 11th, 2012

Windows NT supports multitasking with the help of preemptive multitasking which is based on preemptive operating system. The operating system takes control of the processor from a task in two ways: W...

Answered by: peter on: Nov 1st, 2005

Preemptive multitask

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

Asked By: Ralf Krauseman | Asked On: Jun 3rd, 2007

Answered by: Sandhya.Kishan on: Jul 11th, 2012

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

Answered by: coolsam300 on: Jun 21st, 2012

Sed -n 2p

How would you get the character positions 10-20 from a text file?

Asked By: Interview Candidate | Asked On: Jun 27th, 2005

Star Read Best Answer

Editorial / Best Answer

Answered by: Suman Saha

Answered On : Jun 27th, 2005

cat filename.txt | cut -c 10-20

Answered by: Ming on: Jun 8th, 2012

Sed -n 25p file_name

Answered by: Jagadeesh on: May 30th, 2012

Cut -c 10-20 filename

How do you read arguments in a shell program - $1, $2 ?

Asked By: Interview Candidate | Asked On: Jul 5th, 2005

Answered by: Mo on: Apr 20th, 2012

Positional parameters are reads either by displaying there value or can be assigned to another variables.

echo $1 $2 $9 ${10} ${20}
shift operators can be used to move around the positional parameters.

example: test.sh 1 2 3 4 5

a=$1

while [ -z "$a" ]
do

echo $a
shift

done

Answered by: rks03075 on: Jun 4th, 2010

Echo $1echo $2

What does $# stand for?

Asked By: Interview Candidate | Asked On: Jul 29th, 2005

Answered by: Mo on: Apr 20th, 2012

$# : Stands for counts of the number of the positional parameters passed to a script.

example test.sh 1 2 3 4 5 6 7 , there are 7 paramaters as passed so the count of the $# is 7

Answered by: manu_khatri on: Feb 18th, 2011

By using $# parameter, the program checks whether the right number of arguments have been entered.

How will you list only the empty lines in a file (using grep)?

Asked By: Interview Candidate | Asked On: Jul 9th, 2005

Answered by: Mo on: Apr 20th, 2012

Grep "^$" filename

Answered by: karthikeyan on: Jul 23rd, 2011

Grep -c "^$" file name


here -c option count the no.of empty line..the anchors denotes the empty line from your file name

How to find how many users have logged in and logged out in last five minutes using shell scripts?

Asked By: hankypanky | Asked On: Jul 17th, 2008

How many users have logged in and logged out in last five or 10 minutes

Answered by: sonia on: Feb 19th, 2012

Current_time=`date +%H%M`
who -u | awk $Current_time - $4

Answered by: Disna on: Jan 6th, 2012

Last -5|grep -i "logged in"|wc -l
last -5|grep -v "logged out"|wc -l

First | Prev | | Next | Last Page

 

 

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.