Unix Basic Question
Unix Basic Question
Unix Basic Question
Que:- What is the difference between a shell variable that is exported and the one that is not
exported?
Ans:-
If we export a variable in shell then we access a variable in the child shell, if not export we can use
that variable only in current shell.
Que: - If you have a string "one two three", which shell command would you use to extract the
strings?
Ans:-
To do this we can use: "cut", "awk", "gawk", "sed" or "shell script".
Que:- How will you list only the empty lines in 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:- When you login to c shell, which script would be run first?
Ans:-
.profile and .tchrc will be executing first.
Que:- How would you get the character positions 10-20 from a text file?
Ans:-
cut –c10-20 file.txt
Que:- How would you print just the 25th line in a file?
Ans:-
Awk ‘NR==25 {print $0}’ file_name
sed –n ‘25p’ file.txt
cat file_name|tail -25|head -1
Que:- What are the different kinds of loops available in shell script ?
Ans:-
for, while, until, select loops.
Que:- How would you replace the “n” character in a file with some “xyz”?
Ans:-
sed ‘s/n/xyz/’ file.txt or ‘s/n/xyz/g’ file.txt 'g' --global.
In vi –
1,$s /n/xyz/g
Que:- How do you take a single line of input from the user in a shell script?
Ans:-
we have to “read” command keyword from user input.
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:- Using the man pages, find the correct ioctl to send console output to an arbitrary tty?
Ans:-
ioctl > /dev/pts/3 via this you Can send the command output to another terminal.
Que:- What happens to a child process that parent process dies. what’s bad about this?
Ans:-
Nothing going bad, Because the child process is adopted by init process. When its parent Process dies.
Que:- Write a script to send mail from three other machines on the network to root at the machine
you’re on. Use a ‘here doc’, but include in the mail message the name of the machine the mail is
sent from and the disk utilization statistics on each machine?
Ans:-
Que:- Why can’t root just cd to someone’s home directory and run a program called a.out sitting
there by typing “a.out”, and why is this good?
Ans:-
Que:- How do you fix a problem where a printer will cut off anything over 1MB?
Ans:-
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 the Disk Usage of file sizes under each directory in currentDirectory ?
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:- How to know the date & time. When script is 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 the current directory you’re in?
Ans:-
pwd
Que:- How do you search for a string in a directory with the subdirectories recursed?
Ans:-
find /path/name –name filename*|xargs grep word
Que:- How do you stop all the processes, except the shell window?
Ans:-
Que:- How do you fire a process in the background?
Ans:-
sh filename.sh & OR sh filename.sh Press Enter button and then type “bg” command.
Que:- How do you find out the number of arguments passed to the shell script?
Ans:-
echo $#
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:- 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:-
Just use a find command Like:
find /path/path/ -type d –perm -222 –print
So, via this u can get the information.
Que:- In a single command how do you run the previous command in the command prompt.
Ans:-
!n --n where “!” for last execution command where command name start from “n” character.
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:- How do you list the files in an UNIX directory while also showing hidden files?
Ans:-
ls –ltra
Que:- How do you execute a UNIX command in the background?
Ans:-
Use the "&"
Que:- What UNIX command will control the default file permissions when files are created?
Ans:-
Umask
Que:- Explain the read, write, and execute permissions on a UNIX directory.
Ans:-
Read: allows you to see and list the directory contents.
Write: allows you to create, edit and delete files and subdirectories in the directory.
Execute: gives you the previous read/write permissions plus allows you to change into the directory
and execute programs or shells from the directory.
Que:- the difference between a soft link and a hard link?
Ans:-
A symbolic (soft) linked file and the targeted file can be located on the same or different file system
while for a hard link they must be located on the same file system.
Que:- Give the command to display space usage on the UNIX file system.
Ans:-
df –lk
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:- What is the use of "shift" command in passing parameters?
Ans:-
Que:- What is the need of including script interpreter in your shell script?
Ans:-
Que:- When you login to a c shell, which script would be run first?
Ans:- .profile
Que:- What are the three main forms of enabling debugging in a shell script?
Ans:-
Que:- What does a "b" as the first letter of a file's permissions mean?
Ans:- block special file.
Que:- If I'm logged in as root, what command lets me "become" another user id?
Ans:- su – user_name
Que:- What command will tell me how busy the system is? (load average)
Ans:- top, prstat,mpstat, iostat
Que:- What's the startup files for the C shell (or Bourne)?
Ans:- .login.cshrc file
Que:- What is /etc/aliases?
Ans:-
Que:- How can we find out What's the current version of your OS?
Ans:- uname –r
Que:- What is the first process to start on a UNIX system, after the kernel?
Ans:- init , shedular
Que:- Who is CERT? What do they do? Computer Emergency Response Team, issue advisories
Ans:-
Que:- Explain the syntax of command used for renaming the file?
Ans:- mv command
Que:- Which command is used to list file in given directory along with permission?
Ans:-
Que:- How to know how many users are currently log in?
Ans:-
Que:- Which commands are used to perform copy & paste operations in vi editor?
Ans:-
Que:- What are the various options that can be used with ls command?
Ans:-
Que:- What is inode table? What all information it consist of regarding files?
Ans:-
Que:- How can we declare in variable as integer in bash shell or declared in korn shell as integer?
Ans:-
Que:- What is the need of including script interpreter in your shell script?
Ans:-
Que:- How to create environment variables?What are the conditions for creating variables?
Ans:-
Que:- When you login to a c shell, which script would be run first?
Ans:-
Que:- What are the three main forms of enabling debugging in a shell script?
Ans:-
Que:- What does a "b" as the first letter of a file's permissions mean?
Ans:-
Que:- If I'm logged in as root, what command lets me "become" another user id?
Ans:-
Que:- What command will tell me how busy the system is? (load average)
Ans:-
Que:- What's the startup files for the C shell (or Bourne)?
Ans:-
Que:- How can we find out What's the current version of your OS?
Ans:-
Que:- What is the first process to start on a UNIX system, after the kernel?
Ans:-
Que:- Who is CERT? What do they do? Computer Emergency Response Team, issue advisories
Ans:-
Que:- Explain the syntax of command used for renaming the file?
Ans:-
Que:- Which command is used to list file in given directory along with permission?
Ans:-
Que:- How to know how many users are currently log in?
Ans:-
Que:- Which commands are used to perform copy & paste operations in vi editor?
Ans:-
Que:- What is system calls? System call provide interface between programme and kernel.
Ans:-
Que:- What are the various options that can be used with ls command?
Ans:-
Que:- What is inode table? What all information it consist of regarding files?
Ans:-
Que:- How can we declare in variable as integer in bash shell or declared in korn shell as integer?
Ans:-
Que:- What is use of character special file and how it’s create?
Ans:-
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 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 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.