Unix Commands
Unix Commands
Unix Commands
then
command(s)
fi
If the value stored in the variable mydir exists and is a directory file
, the command(s) located between then and fi will be executed.
(6.) How do you access command line arguments from within a shell script?
Arguments passed from the command line to a shell script can be accessed
within the shell script by using a $ (dollar sign) immediately followed with th
e argument's numeric position on the command line.
Details:
For example, $1 would be used within a script to access the first argume
nt passed from the command line, $2 the second, $3 the third and so on. Bonus: $
0 contains the name of the script itself.
(7.) How would you use AWK to extract the sixth field from a line of text contai
ning colon (:) delimited fields that is stored in a variable called passwd_line?
echo $passwd_line | awk -F: '{ print $6 }'
Details:
Consider this line of text stored in the variable $passwd_line $ echo $passwd_line
mail:x:8:12:mail:/var/spool/mail:
$
Here : is delimiter
(8.) What does 2>&1 mean and when is it typically used?
The 2>&1 is typically used when running a command with its standard outp
ut redirected to a file.
For example, consider:
command > file 2>&1
Anything that is sent to command's standard output will be redirected to
"file" in this example.
The 2 (from 2>&1) is the UNIX file descriptor used by standard error (st
derr). Therefore, 2>&1 causes the shell to send anything headed to standard erro
r to the same place messages to standard output (1) are sent...which is "file" i
n the above example.
To make this a little clearer, the > in between "command" and "file" in
the example is equivalent to 1>.
(9.) What are some ways to debug a shell script problem?
One method specific to shell scripting is using "set -x" to enable debug
ging.
Details:
Consider the following shell script example (notice that "set -x" is com
mented out at this time):
#!/bin/ksh
#set -x
i=1
while [ $i -lt 6 ]
do
print "in loop iteration: $i"
((i+=1))
done
exit
This script will produce the following output:
$ ./script1
in loop iteration:
in loop iteration:
in loop iteration:
in loop iteration:
in loop iteration:
$
1
2
3
4
5
If we uncomment (remove the #) from the "set -x" line in the script, thi
s is what is displayed when the script runs:
$ ./script1
+ i=1
+ [ 1 -lt 6 ]
+ print in loop iteration:
in loop iteration: 1
+ let i+=1
+ [ 2 -lt 6 ]
+ print in loop iteration:
in loop iteration: 2
+ let i+=1
+ [ 3 -lt 6 ]
+ print in loop iteration:
in loop iteration: 3
+ let i+=1
+ [ 4 -lt 6 ]
+ print in loop iteration:
in loop iteration: 4
+ let i+=1
+ [ 5 -lt 6 ]
+ print in loop iteration:
in loop iteration: 5
+ let i+=1
+ [ 6 -lt 6 ]
+ exit
$
EXIT;
EOF`
$> echo $SqlReturnMsg
(14.)How to fail a shell script programmatically?
Just put an [exit] command in the shell script with return value other t
han 0. This is because the exit codes of successful Unix programs is zero. So, s
uppose if you write
exit -1
inside your program, then your program will thrown an error and exit imm
ediately.
(15.)How to check if the last command was successful in Unix?
To check the status of last executed command in UNIX, you can check the
value of an inbuilt bash variable [$?].
See the below example:
$> echo $?
If it return 0 then last command is successful.
(16.)How to check if a file is present in a particular directory in Unix?
Using command, we can do it in many ways. Based on what we have learnt s
o far, we can make use of [ls] and [$?] command to do this.
See below:
$> ls l file.txt; echo $?
If the file exists, the [ls] command will be successful. Hence [echo $?]
will print 0. If the file does not exist, then [ls] command will fail and hence
[echo $?] will print 1.
(17.) Sample script to connect DB from UNIX & run PL/SQL
#!/bin/ksh
# ######################################################################
#####
# DESCRIPTION
# - Sample script to connect database from unix and run sql, pl/sql
# PARAMETER : empno
# ######################################################################
#####
#Path for Spool Log
SpoolLog=${BDR_SPL}/sample_script_db_`date '+%d-%m-%Y-%H-%M-%S`.log
in_empno=$1
echo "Empolyee Number Entered: "$in_empno
#SqlReturnMsg=`sqlplus -s username/password@database<<EOF`
sqlplus -s ${BDR_ORA_USR_OWN}/${BDR_ORA_PWD_OWN}<< EOF
WHENEVER SQLERROR EXIT FAILURE ROLLBACK;
WHENEVER OSERROR EXIT FAILURE ROLLBACK;
SET SERVEROUT ON SIZE 100000;
emp.empno%TYPE;
emp.ename%TYPE;
emp.sal%TYPE;
emp.deptno%TYPE;
BEGIN
SELECT empno, ename, sal, deptno
INTO
lv_empno, lv_ename, lv_sal, lv_deptno
FROM emp
WHERE empno = ${in_empno};
DBMS_OUTPUT.PUT_LINE('empno: '||lv_empno||' ename: '||lv_ename||' sal: '
||lv_sal||' deptno: '||lv_deptno);
END;
/
SPOOL OFF;
EXIT;
EOF
error=`grep -i "error" ${SpoolLog}`
[ "A${error}" != "A" ] && exit 1
(18.)Special
-> $
-> ?
-> !
Shell Variables
Contains the process identification number of the current process
Contains the exit status of the most recent foreground process
Contains the process ID of the last background job started
-------------------------------Unix Command
-----------(1.) How to print/display the first line of a file?
$> head -1 file.txt
$> head -2 file.txt
--1st Line
--1st two Lines
t
(12.)How to show the non-printable characters in a file?
Open the file in VI editor. Go to VI command mode by pressing [Escape] a
nd then [:]. Then type [set list]. This will show you all the non-printable char
acters, e.g. Ctrl-M characters (^M) etc., in the file.
(13.)How to zip a file in Linux?
Use inbuilt [zip] command in Linux
$> zip file.txt
$> gzip file.txt
(14.)How to unzip a file in Linux?
Use inbuilt [unzip] command in Linux.
$> unzip j file.zip
$> gunzip file.gz
touch file
chmod 400 file
(20.) How will you find which operating system your system is running on in UNIX
?
By using command "uname -a" in UNIX
$>uname -a
SunOS gcrdevap01 5.10 Generic_147440-13 sun4u sparc SUNW,SPARC-Enterpris
e
(21.) How will you run a process in background? How will you bring that into for
eground and how will you kill that process?
For running a process in background use "&" in command line.
For bringing it back in foreground use command "fg jobid" and for gettin
g job id you use command "jobs", for killing that process find PID and use kill
-9 PID command.
This is indeed a good Unix Command interview questions because many of p
rogrammer not familiar with background process in UNIX.
(22.) How do you know if a remote host is alive or not?
You can check these by using either 'ping' or 'telnet' command in UNIX.
$>ping host_name
(23.) How do you see command line history in UNIX?
$> history
(24.) When an user logged in unix account, which startup script runs first?
- /etc/profile runs first when a user logs in.
- $HOME/.profile runs second when a user logs in.
- $HOME/.kshrc runs third if the ENV variable is set.
(25.) Which operator is used to combined unix command?
(26.) grep
-i
-c
-l
-v
-n
Options
Ignores uppercase and lowercase
Prints a count of lines that match
Prints the name of the files in which the lines match
Prints the lines that do not contain the search pattern
Prints the line numbers