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 replace the n character in a file with some xyz?

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

Answered by: Rapolu Venkateshwarlu on: Dec 12th, 2013

nw=`cat filename|cut -f -d `|sed -i "s/$nw/

Answered by: shanmugaraj on: Nov 14th, 2013

sed s/n/xyz/g filename

n -character want to replace
xyz - new character/word to insert

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 "

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: Rajendra Prasad on: Jun 7th, 2013

head -25 filename|tail -1

Answered by: Rajendra Prasad on: Jun 5th, 2013

sed -n 25p filename(a.txt)

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 <--from linux

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.

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: santarvedi on: Oct 30th, 2012

i think by modifying the IFS, we can achieve this as follows, i hope this helps. #take for example there is an entry in /etc/password with this info santarvedi:x:500:500:santarvedi:/home/santarvedi:/...

Answered by: Sijeesh KT on: Aug 18th, 2012

perl -ane print "$F[0]"; < test.txt

You can use this I hope , replace the zero with the column number you want to print.Normally people use cut and awk command for extracting column.

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

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: gururaj on: Jun 5th, 2012

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

Answered by: Jessmon on: Jun 1st, 2012

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

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 $1
echo $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 <= 5 { print }

Answered by: Disna on: Jan 6th, 2012

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

How do you invoke and send mail from UNIX ?

Asked By: anandvoona | Asked On: Jun 10th, 2011

Answered by: Mohankumar on: Feb 14th, 2012

Code
  1. mailx -s "<subject>" <email_id list> < <input_file>

Answered by: Satya Sai Behera on: Nov 23rd, 2011

The following is a more detailed example for using mailx in shell script. This should help.

Code
  1. SUBJECT="Email Subject Line"
  2. TO=xyz@company.com
  3. CC=`tr '
  4. ' ', ' < mailCClist.txt`
  5. #BCC="xyz2@comp.com"
  6.  
  7. echo "Send the E-mail message..."
  8. /usr/bin/mailx -s "${SUBJECT}" ${TO} <<-END
  9.  
  10. ~< ! uuencode $DCdir/$FileName "Result.txt"
  11.  
  12. ~c ${CC}
  13. ~b ${BCC}
  14.         Hi,
  15.  
  16.         Blah, Blah.. body of the email.
  17.  
  18.         Thanks
  19.         Satya
  20.         ~.
  21.         END
  22.  
  23. echo "Mail Sent!!"
  24.  

Syntax for mailx command

Asked By: shalinik.sdm | Asked On: Jun 20th, 2011

Hello,i am trying to write a UNIX shell script program to send mail to cc and to list.I am using the '-c' option for mailx UNIX command,but am getting an error illegal -c option. Could any one please guide me to use the mailx command in UNIX shell scripting to send the mail to the cc and to list along...

Answered by: Mitra on: Nov 24th, 2011

Code
  1. uuencode attachment1 attachment1 | mailx -c x@yz.com,a@bc.com -s "subject" m@no.com

Answered by: Satya Sai Behera on: Nov 23rd, 2011

You should be able to use something like the following example to get the CC option working with mailx. Preferably create a list of users needed to be put in the CC option (as mailCClist.txt in the ex...

Database connecion

Asked By: pranoy | Asked On: Apr 25th, 2009

How will you write a shell script to connect to SQL database?

Answered by: Ashutosh Gupta on: Sep 10th, 2011

in vi editor
vi connect.sh
#!/bin/bash

sqlplus -s sys as sysdba < startup
spool on;
spool ashu;
connect scott/tiger
select * from emp;
spool off;
exit;
EOF
cat ashu

Answered by: NARESH on: Jul 31st, 2011

sqlplus -s username/password@schema name
:wq

Read jar files

Asked By: karthikn | Asked On: Jul 19th, 2011

How to read the contents of fie inside jar without extracting in shell script?

Answered by: Reema on: Aug 26th, 2011

You can read the contents of file inside jar without extracting in shell script by using:

tar -tvf file.tar

Answered by: Pradeep Shenoy on: Aug 21st, 2011

jar tvf jarName.jar

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.