GeekInterview.com
Series: Subject: Topic:

Shell Scripting Interview Questions

Showing Questions 1 - 20 of 54 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: Patricia on: May 25th, 2012

in bash:

cat $filename | awk {print $25 }

Answered by: Govind Kailas on: May 10th, 2012

a simple perl one liner to print only the 25th line

Code
  1. perl -ne $. == 25 && print && exit

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

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: Naresh on: May 2nd, 2012

cat file_name | cut -c 10-20

Answered by: poi677 on: May 1st, 2012

head -20 | tail -10

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: naresh on: May 2nd, 2012

cat file_name | awk {print $n} # where N is the column no. and tab is field seperator

cat SO_02052012_2.csv | awk -F; {print $n} # where N is is column no. and semicolon is field seperator

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: Mo on: Apr 20th, 2012

path=${1?pls provide the path to script}
cd $path
for i in *
do
mv $i ${i}.new
if [ $? -eq 0 ]
then
echo "file rename successful"
ls -l ${i}*
fi
done

Answered by: abdas on: Jun 26th, 2011

for file in /dxusers/testing/test_delete/*
do
 if [ -f $file ]
 then
  mv $file $file.new
 fi
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.

What does $? Return?

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

Answered by: Mo on: Apr 20th, 2012

$? returns the status of the last command executed . if the last command is successfull then it will 0 , otherwise it will retrun non-zero. ex: date ; echo $? or try this dte ; echo $?

Answered by: andy on: Jan 28th, 2012

UNIX commands (well the "well behaved" ones) give a return code between 0 and 127 (inclusive). General convention is that a return code of 0 (zero) indicates success and that a non-zero return code i...

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 extract the second row of a text-file?

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

Answered by: Debi Maharana on: Apr 7th, 2012

awk NR==

Answered by: Furkan on: Mar 30th, 2012

cut -d -f2

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

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

Answered by: Furkan on: Mar 30th, 2012

:1,$s/n/xyz/g

Answered by: oceanblue on: May 16th, 2010

All the sed replacements are by default global. It's in the vi editor that you have to specifically give "g" for global replacement.

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

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: shilpa keesara on: Aug 7th, 2011

Using KSH while loop#!/bin/ksh
#Get the count of total number of arguments passed while executing this script
int i = $#; #total number of arguments passed#
names=$@; #list of actual arguments passed#
while [ ${i} -gt 0 ]
do
echo '${names[i]}';
i=`expr i - 1`;
done
exit 0;

Answered by: abdas on: Jul 26th, 2011

$ cat rev2
i=$#
echo "$@" > file_1

while [ $i -gt 0 ]
do
cut -f $i -d" " file_1 >> file_2
i=`echo $i-1|bc`
done

echo `cat file_2`;
rm -f file_1 file_2;

$ sh rev2 A B C D E
E D C B A

If you have a string "one two three", which shell command would you use to extract the strings?

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

Answered by: abdas on: Jul 25th, 2011

Code
  1. echo $1
  2. echo $2
  3. echo $3

Answered by: iamjogi on: Aug 23rd, 2009

Try this:

echo "one two three" | xargs

the output is: one two three

UNIX security mechanisms

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

What are the different security mechanisms available in UNIX?

Answered by: Gunjan David on: Jun 27th, 2011

a) vi /etc/default/login
comment the line of /dev/console

b)vi .rhosts
enter the hostname by which the only user can connect with your system....bt no one else.....
c) disable the ftp for the selected users in /etc/ftpusers file ...

Answered by: Deepa_0982 on: Jan 21st, 2009

Unix is having 3 ways of Security Mechanism:1. By granting or revoking File permissions. Owner or Admin can change permissions to be given to group or others by using chmod command in Unix.2. Login is...

Shell scripting commands

Asked By: siddhishah_999 | Asked On: Apr 28th, 2009

In shell scripting how to indentify that the previous command was run successfully?

Answered by: abdas on: Jun 26th, 2011

in 2 ways it can be done:
1st way
$ echo $?  

2nd way:

$ pwd 2> error| cat error|wc -l
       0
$ cat h 2> error| cat error|wc -l
       1

pwd or cat or any command if outpt is 0 then successful else failed.

Answered by: Nagunix on: Jun 6th, 2010

Use command

echo $?

at prompt.

If the output of command is 0 than succesfully executed.
If the output is non -zero than not successfully executed.

First | Prev | | Next | Last Page

 

 

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Career Counselling

 Have Career Question?

 Ask Chandra

 Ask Only Career questions.

Follow us:
 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, click "Subscribe".