Вы находитесь на странице: 1из 6

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

ANS
cat filename.txt | cut -c 10-20
How would you print just the 25th line in a file (smallest possible script
please)?
ANS
sed -n '25p' filename.txt

How To Connect To Data Base in Solaris


echo "Enter User Name "read nameecho "Enter passwd "stty -echoread passstty echo sqlplus -s
$name@oracle/$pass<
How would you print just the 25th line in a file (...
head -25 file| tail -1 goes wrong because if some one deletes some lines even then it gives last
line as 25th line.
so the right option is

tail +25 file| head -1

tail -n +25Â | head -1 OR


head -n 25 | tail -1

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

sed 's/n/xyz/g' filename > new_filename

RE: How would you replace the n character in a file wi...


vi file name

then go to command mode

%s/n/xyz/g
How would you replace the n character in a file wi...

tail -1 sample.txt | sed 's/.$/xyz/'Check this

What does $? return?

Will return the status of the command which is executed lastly.


0 => Success
2 => Error
$? will return exit status of command .0 if command gets successfully executed ,non-zero if
command failed.
What does $# stand for?
$# returns the number of parameters that are passed to a shell script
$? returns the exit code of the last executed command (0 : Successful, 1 or other: Failed)
What is $*?
Will display all the commandline arguments that are passed to the script
Its mainly used for showing up all params.
i.e - if you execute script in following way to which we are trying to pass 4 params
./test.sh 1 "hello world" 2 "test"
and if we print inside script
echo $*
it will print
null1 hello world 2 testnull
$1: the 1st parameter
$2: the 2nd parameter
...
$*
All parameters in the command line
$* displays values of all positional parameters set
If you have a string "one two three", Which shell command would you
use to extract the strings?
echo "one two three" | cut -d" " -f 1,2,3
or
echo "one two three" | awk '{print $1 $2 $3}'

echo $string | cut -d" " -f1


echo $string | cut -d" " -f2
echo $string | cut -d" " -f3

echo "one two three" | cut -d" " -f 1,2,3


or
echo "one two three" | awk '{print $1 $2 $3}'

sed and awk

echo "one two three" | while read ONE TWO THREE


print $ONE
one
print $TWO
two
print $THREE
three
What is the difference between a shell variable that is exported and
the one that is not exported?

try this simple script it will clear your concept :-

script a.sh :-
----------
export A=3
B=5
sh b.sh
--------

script b.sh :-
-----------
echo "exported variable $A is :- $A"
echo "Variable $B -- $B "

---------

A persists in b.sh whereas B doesn't.

exported variable is visible to the child processes while the normal variables are not.
The Shell variable which is exported would available to all the programs outside the Shell also.
And the shell variable which is not exported, would available for that shell or for the shell
program only, in which the variable is declared.

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

grep "^$" filename.txt


OR
grep -c "^$" filename.txt
OR

grep "^[ ]*$" filename.txt

In character set (between [ and ] one space and tab is given)

this command will gives all the blank line including those having space and tabs (if
pressed)only

OR
We can do this efficiently through awk script

awk '{
if (NF == 0)
{
print "Here comes the empty line"
print $0
}
}' filename

NF=number of fields in the current line


$0=current line

How do you schedule a command to run at 4:00 every morning?

by using cron job


OR
Schedule the Job using crontab..From the command prompt perform a "man" on crontab and
then type in crontab -e to create a new "job"
OR
do first

crontab -e and then add a line like below

0 4 * * * name of the command


OR
Step 1. Set environemen variable EDITOR=vi if not set

Step 2. Give command crontab -e

Step 3. Add following entry at the end

0 4 * * * <Your command to run at 4:00 am morning>

OR

This is done by 'at' command in HPUX and Sun solaris or 'cron' command can be used too.
Mukesh Chauhan –Bangalore

1. There are two ways to schedule a script to run exactly at 4:00 AM every morning

a: CRONTAB
b. AT (at command executes only once)

Crontab format
* * * * * <command>
<minute> <hour> <date> <month> <day_of_the_week> command
<0-59> <0-23> <1-31> <1-12> <0-7> command
OR

1st step
$ cat > cmdfile
//<Minute> <Hour> <Day Of Month> <Day Of Week> <Command>
0 4 * * <Command>
Ctrl d

2nd Step

crontab cmdfile

How do u open a read only file in Unix?


vi filename
e.g vi abc.txt but you cannot write to it

easy way if file is small


cat filename and contents is display on screen
OR

view <filename> is a better option. It will not allow writing to a file normally, unless done
forcefully like "wq!" or "x!"...

"vi -R filename"

OR

In a shell script, you can open a file in read only mode by using O_RDONLY

Syntax - open(<filename>,O_RDONLY)
more <file_name> is the best option.
OR

If you mean open a file in Unix which has read-only permission

... then you open it the same way you open any other file. The system will simply prevent you
from writing to it.

If you mean open a file in Unix such that you can not write to the file, even though you
otherwise would be allowed because you do have write permission ...

... then it depends on what mechanism you use to open the file. Others have already answered
for opening with the "vi" editor. Other programs, such as "emacs", or other text editors, would
have other ways.
What are the different kinds of loops available in shell script?
for, if, while, case
Broadly categorised in 3
for
while
until
How do you read arguments in a shell program - $1, $2 ?
#!/bin/sh
for i in $*
do
echo $i
done

On executig the above script with any number of command-line arguments it will display all the
parametsrs.
OR
$1 would be the first command line argument, $2 the second, and so on
$0 is the name of the script or function
OR
Shell script accepts parameters in following format...
$1 : first
$2 : second....so on upto
$9 : 9th param
whereas $0 : gives script/function name
If your script has more than 9 params then accept in following way...
${12} : 12th param
${18} : 18th param
OR

by using :

exec<$1 or exec<$2
When you login to a c shell, which script would be run first? (before
the terminal is ready for the user)
For C shell ,
first /etc/.login script is run & after that
~/.login is run & then ~/.cshrc is run.
OR

.profile file will execute first when a user logs in.


OR
For C shell ,
first /etc/.login script is run & after that
~/.login is run & then ~/.cshrc is run.

Вам также может понравиться