UNIX Bids

Que:- How to list only empty lines from a file?
Ans:-
1.     cat file|sed –n ‘/^$/p’|wc –l
2.     sed –n ‘/^$/p’ file |wc –l
3.     sed ‘/^$/!d’ file |wc –l
4.     awk ‘/^$/’ file|wc –l
5.     grep –nv ^$ file
6.     grep -nv "." file

Que:- How to cut character positions 10-20 from a text file?
Ans:-
cut –c10-20 file.txt

Que:- How would you print only 25th line from a file?
Ans:-
sed –n ‘25p’ file.txt

Que:- How would you replace “n” character with some “xyz” in file?
Ans:-
sed ‘s/n/xyz/’ file.txt
sed ‘s/n/xyz/g’ file.txt – replace character in entire file.

Que:- Write a script to convert all DOS style backslashes to UNIX style slashes in a list of files?
Ans:-
1.we can use "dostounix" and "unixtodos" command.
2.we can do this for sed ‘|\|/|g’ filename.
3.cat hello|while read line
   do
      b=`echo $line|tr -cd ',' |wc -c`
      i=1
      while [ $i -le $b ]
      do
         cc=`echo $line|cut -d "," -f$i`
         ee=`echo -e $cc\/`
         echo -e "$ee\c"
         i=`expr $i + 1`
      done
   done

Que:- Write a regular expression (or sed script) to replace all occurrences of the letter ‘f’, followed by any number of characters, followed by the letter ‘a’, followed by one or more numeric characters, followed by the letter ‘n’, and replace what’s found with the string “UNIX”.
Ans:- 
sed –e ‘s/f/rr/g’ –e ‘s/a/1234/g’ –e ‘s/n/UNIX/g’ file.txt


Que:- Write a Unix e-mail commands.?
Ans:-
Example:
echo "something"|mailx -s "subject" chrajeshdagur@gmail.com
cat ${LOGFILE}|mailx -s "Subject: Hello on `date`" chrajeshdagur@gmail.com

Que:- List the files in current directory sorted by size ?
Ans:- 
ls –lS
ls -l | grep ^- | sort -nr

Que:- List the hidden files in current directory ?
Ans:-
ls –la
ls -a1 | grep "^\."

Que:- Delete blank lines in a file ?
Ans:-     
grep –v ^$ filename ’ > new_sample.txt
Sed ‘/^$/d’filename

Que:- Search for a sample string in particular files ?
Ans:-
grep –w “string” filename

Que:- Display the last newly appending lines of a file during appending data to the same file by some processes ?
Ans:-
tail-f filename

Que:- Display Disk Usage of file sizes under each directory from current Directory ?
Ans:-     
du -k * | sort -nr (or) du -k  | sort –nr
du –h *|sort –rn|head -1 OR in the current directory we use ls –lShr
OR find ./ -printf ‘%s %p\n’|sort –rn|head -5

Que:- Display the all files recursively with path under current directory ?
Ans:-   
find . -depth –print
find . –type f -print

Que:- Set the Display automatically for the current new user ?
And:-
In the user .{shell}rc file or in the .profile we need to add this entry
export DISPLAY=hostname:0.0
export DISPLAY=`eval ‘who am i | cut -d"(" -f2 | cut -d")" -f1′`Here in above command, see single quote, double quote, grave ascent is used. Observe carefully.

Que:- Display the processes, which are running under your username ?
Ans:-
ps –aef|grep –w “username”

Que:- List some Hot Keys for bash shell ?
Ans:-    
Ctrl+l -- Clears the Screen.
Ctrl+r -- Does a search in previously given commands in shell.
Ctrl+u -- Clears the typing before the hotkey.
Ctrl+a -- Places cursor at the beginning of the command at shell.
Ctrl+e -- Places cursor at the end of the command at shell.
Ctrl+d -- Kills the shell.
Ctrl+z -- Places the currently running process into background.

Que:- Display the files in the directory by file size ?
Ans:-
find ./ -printf “%s %p\n”

Que:- How to save man pages to a file ?
Ans:-   
man top | col -b > top_help.txt

Que:- How to know the date & time of script When it was executed?
Ans:-    
ls –lu -- It show access time of script
Add the following script line in shell script.sh echo "Script is executed at `date`".

Que:- How do you find out drive statistics in UNIX OS?
Ans:-
iostat –x or -E

Que:- How to Display disk Sizes in KB, MB and GB ?
Ans:-
du –k : Show size in KB
du –h : Show size in GB

Que:- Display top ten largest files/directories ?
Ans:-    
ls –lS|head -10
ls -l|awk -F" " ' {print $5, $0}'|sort -rn|head -10|awk -F '{print $1}'   
du -sk * | sort -nr | head

Que:- What’s the command to find out users on the system?
Ans:-
cat /etc/passwd|awk –F”:” ‘{print $1}’

Que:- How do you count words, lines and characters in a file?
Ans:
Using “wc” command with -w,-c and -l options.

Que:- How do you search for a string inside a given file?
Ans:-
Using “grep” and "egrep" command.
Using grep "string" * - it'll search string only current dir of all files.

Que:- How do you search a string in multiple files of subdirectories?
Ans:-
find /path/name –name filename*|xargs grep word

Que:- How to run a Script in background?
Ans:-
nohup sh filename.sh > filename.log & OR

Que:- How do you test for file properties in shell scripts?
Ans:-
we use the options in the if block are ::
-s:- check if file size is greater than zero.
-z:- check if the string size is null.
-n:- True if the length of string is non-zero.
-f:- check it is file or not  and check is exist or not.
-d:- check the directory.
-r:- if the file exist and read-only mode.
–w:- if the file exist and writable mode.
-x:-  if the file exist and executable mode. Etc

Que:- How does a case statement look in shell scripts?
Ans:-
case $i in
1)    echo hello one;;
2)    echo hello two;;
3)    echo hello three;;
*)     echo defult;;
esac
Que:-  How do you define a function in a shell script?
Ans:-
#!/bin/bash
function name 
{
       echo “Hello Dear are you Ok.”
}
OR 
name()
{
    Statements
}

For calling the function if u define a function in file.env file the you have to source the file in the execution script and call the function by its name.
For Example:
vi file.sh
. /path/file.env – source the .env file.
name – Name of the function.
~
~
:wq

Que:- How does getopts command work?
Ans:-
getopts obtains options and their arguments from a list of parameters that follows the standard POSIX. Typically, shell scripts use getopts to parse arguments passed to them. When you specify args on the getopts command line, getopts parses those arguments instead of the script command line.
For Example:
while getopts d:s o
do
case "$o" in
d) seplist="$OPTARG";;
s)  paste=hpaste;;
[?]) print >&2 "Usage: $0 [-s] [-d seplist] file ..."
      exit 1;;
esac
done

Each time it is invoked, the getopts utility shall place the value of the next option in the shell variable specified by the name operand and the index of the next argument to be processed in the shell variable OPTIND . Whenever the shell is invoked, OPTIND shall be initialized to 1.
When the option requires an option-argument, the getopts utility shall place it in the shell variable OPTARG . If no option was found, or if the option that was found does not have an option-argument, OPTARG shall be unset.

Que:- The parameters to your script can be passed as -n 15 -x 20. Inside the script, you can iterate through the getopts array as while getopts n:x option, and the variable $option contains the value of the entered option.
Ans:-   
#!/bin/bash
while getopts n:x option
do
        case $option in
                n) echo this is a $OPTARG;;
                x) echo this VB $OPTARG;;
                *) echo default;;
        esac
done

Que:-  How do you export a function in unix ?
Ans:-    
#!/bin/bash
echo “The program of Function Calling”
function fun1()
{
                echo “Hello dear are you OK”
}
export –f fun1
fun1 --call function on prompt.
OR
Inside file we need to source that file like:
. function_file_name –- “ .” is the source operator
sh call.sh –execution of script.


Que:- write a shell script to check whether all the directories in the path exist has read and write permission Explain how you Automate your application using Shell scripting.
Ans:-
find /path/path/ -type d –perm -222 –print

Que:- How u convert string "hi pravin how are you?" to "Hi Pravin How Are You?"
Ans:-
echo “hello unix programming” |sed 's/ [a-z]/\U&/g'|sed 's/^[a-z]/\U&/g'

Que:- How can I Debug a shell scripts and Perl scripting. (at the compile time error or run time error) in Unix environment ?
Ans:-
sh –n filename.sh – compile time for syntax error.
set –x in shell script for debugging script.
perl –d program.pl

Que:- How to delete all the files with extension .dat from a directory tree from root to third level in a single unix command?
Ans:-
Specified the three level directure in the file.lst file and write the file.sh script
cat file.lst|while read line
do
        cd $line
        rm *.dat
done

OR
find /path/ –maxdepth 3 –exec rm –f {} /;

Que: - Create a bash shell script to sort and then uniq the file from the command line and store it to a new file and output the results to the screen. Name this script "sortAndUniq.sh"
Ans:-
cat file.txt|sort|uniq 2> outputfile.txt ; cat outputfile.txt

Que:- Explain iostat, vmstat and netstat ?
Ans:-
Iostat: reports on terminal, disk and tape I/O activity.
Vmstat: reports on virtual memory statistics for processes, disk, tape and CPU activity.
Netstat: reports on the contents of network data structures.

Que:- How would you change all occurrences of a value using VI?
Ans:-

Use :%s///g

Que:- Find a particular string in the all current directory file?
Ans:-
find ./ -type f|xargs grep word
find ./ -type f –exec  grep hello ‘{}’ \;


Que:- See only files name from the “ls” command.
Ans:-
ls –l |grep –i  ^-
ls –l |grep –v ^d
ls –p |grep  -v /

Que:- what is the use of mkdirhier and mkdir -p ?
Ans:-
Create a multiple sub directorys in one shot
Example:- mkdir –p rr/y/i/{tt,uu,I,kk/jj/kk/}/jj.
            OR mkdirhier rr/y/i/{tt,uu,I,kk/jj/kk/}/jj.


Que:- what does mean total in the output of “ls –l”?
Ans:-
It show the all block size in current directory.

Que:- What is the use of /var/spool/mail/root file on UNIX?
Ans:-
In Unix OS that file is read by the mail command by default the mail are stored in this file.

Que:-How to delete a “n”th character of a line or string?
Ans:-
sed 's/^\(.\{7\}\).\(.*\)/\1\2/'  file.txt
OR
n=2; echo "$line" | sed "s/^\(.\{${n}\}\)./\1/"

echo rajesh|sed -r 's/^(.{4})./\1/'  #it will delete 5 character using parentheses grouping \1 retailed for one group 

Que:- How to delete last two an first two character from a file?
Ans:-
cat file.txt|sed ‘s/^..//
cat file.txt|sed ‘s/..$//’

Que:-How to print second field of a file via sed command?
Ans:-

Que:- How to replace the 3rd character with “-“ character?
Ans:-
echo "Hello word"|sed 's/^\(.\{2\}\)./\1-/'

Que:-How to create a virtual command on unix prompt and then run?
Ans:-
we can do this via “xargs”, “-exec”, “shell name like: ksh, bash, tcsh etc.”
Example:- ls | awk ‘{print “cp “ $1 “ /path1/path2”}’|ksh

Que:- How to find the largest file in current directory and sub directory?
Ans:-
du –h *|sort –rn|head -1 OR In the current directory we can use “ls –lShr
OR find ./ -printf ‘%s %p\n’|sort –rn|head -5

Que:- what are the inode numbers and how to get the information of a file via inode number?
Ans:-
With the help of “ncheck” command. We can use it only by the Super User (root).

Que:- What is the use of “Stat” and “istat“ command?
Ans:-

Que:- What is the difference between “–n”, ”+n” and “n”  in the find command ?
Ans:-
 n – It will look only ‘n’th day of file from the current timestamp?
-n – It will look the files from current timestamp to ‘-n’th day.
+n – It will look all the file after ‘+n’ th day.

Que:- How to set environment in csh shell ?
Ans:- /u01/home/oracle/product/11.1.0.7%setenv ORACLE_SID ROCT2
usabhbmst104:/u01/home/oracle/product/11.1.0.7%

usabhbmst104:/u01/home/oracle/product/11.1.0.7%sqlplus / as sysdba




No comments: