Skip to main content

Basic shell scripting part 2

How do you find out what’s your shell?
Answer: echo $SHELL
What’s the command to find out today’s date?
Answer: date
 What’s the command to find out users on the system?
Answer: who
How do you find out the current directory you’re in?
Answer: pwd
How do you remove a file?
Answer: rm
How do you remove a file?
Answer: rm -rf
How do you find out your own username?
Answer: whoami
How do you send a mail message to somebody?
Answer: mail somebody@techinterviews.com -s ‘Your subject’ -c ‘ cc@techinterviews.com
How do you count words, lines and characters in a file?
Answer: wc
How do you search for a string inside a given file?
Answer: grep string filename
How do you search for a string inside a directory?
Answer: grep string *
How do you search for a string in a directory with the subdirectories recursed?
Answer: grep -r string *
What are PIDs?
Answer: They are process IDs given to processes. A PID can vary from 0 to 65535.
How do you list currently running process?
Answer: ps
How do you stop a process?
Answer: kill pid
How do you find out about all running processes?
Answer: ps -ag
How do you stop all the processes, except the shell window?
Answer: kill 0
How do you fire a process in the background?
Answer: ./process-name &
How do you refer to the arguments passed to a shell script?
Answer: $1, $2 and so on. $0 is your script name.
What’s the conditional statement in shell scripting?
Answer: if {condition} then … fi
How do you do number comparison in shell scripts?
Answer: -eq, -ne, -lt, -le, -gt, -ge
How do you test for file properties in shell scripts?
Answer: -s filename tells you if the file is not empty, -f filename tells you whether the argument is a file, and not a directory, -d filename tests if the argument is a directory, and not a file, -w filename tests for writeability, -r filename tests for readability, -x filename tests for executability
How do you do Boolean logic operators in shell scripting?
Answer: ! tests for logical not, -a tests for logical and, and -o tests for logical or.
How do you find out the number of arguments passed to the shell script?
Answer: $#
What’s a way to do multilevel if-else’s in shell scripting?
Answer: if {condition} then {statement} elif {condition} {statement} fi
How do you write a for loop in shell?
Answer: for {variable name} in {list} do {statement} done
How do you write a while loop in shell?
Answer: while {condition} do {statement} done
How does a case statement look in shell scripts?
Answer: case {variable} in {possible-value-1}) {statement};; {possible-value-2}) {statement};; esac
How do you read keyboard input in shell scripts?
Answer: read {variable-name}
How do you define a function in a shell script?
Answer: function-name() { #some code here return }
How does getopts command work?
Answer: 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.

What’s a way to do multilevel if-else’s in shell scripting?
Answer: if then elif fi
How do you write a for loop in shell?
Answer: for in do done
How do you write a while loop in shell?
Answer: while do done
How does a case statement look in shell scripts?
Answer: case in ) ;; ) ;; esac
How do you define a function in a shell script?
Answer: function-name()
How do you find out about all running processes?
Answer: ps -ag
How do you stop a process?
Answer: kill pid
How do you remove a file?
Answer: rm
How do you remove recursively?
Answer: rm -rf
What are PIDs?
Answer: They are process IDs given to processes. A PID can vary...
How do you list currently running process?
Answer: ps
What is $$?
What is a named pipe?
What does || mean?
What does && mean?
What is a loop?
What does while do?
What is a function? 
What are the different kinds of loops available in shell script?
for if while case

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 does $? return?
Will return the status of the command which is executed lastly.
0 > Success
2 > Error
How do u open a read only file in Unix?
"vi -R filename"
What is the difference between a shell variable that is exported and the one that is not exported?
If you have a string "one two three", Which shell command would you use to extract the strings?
How do you schedule a command to run at 4:00 every morning?
How will you list only the empty lines in a file (using grep)?
grep ^$ filename.txt
When you login to a c shell, which script would be run first? (before the terminal is ready for the user)
first /etc/.login script is run & after that
~/.login is run & then ~/.cshrc is run.
How would you get the character positions 10-20 from a text file?
cat filename.txt | cut -c 10-20
How would you print just the 25th line in a file (smallest possible script please)?
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

Comments

Popular posts from this blog

Basic shell scripting part 1

What is $*? Its mainly used for showing up all params. This show all parameter values passed in shell script What does $# stand for? # will return the number of parameters that are passed as the command-line arguments. What does $? Return? $? will return exit status of command .0 if command gets successfully executed ,non-zero if command failed. What are Different types of shells? sh : the oldest shell  csh : C shell  ksh : Korn Shell  bash : bourne again shell  How do you read arguments in a shell program – $1, $2? Shell script accepts parameters in following format…  $1 would be the first command line argument, $2 the second, and so on  $0 is the name of the script or function If your script has more than 9 params then accept in following way…  ${12} : 12th param  ${18} : 18th param What are the different kinds of loops available in shell script ? for, if, while, case What is the difference...

Linux and Maven Commands

Maven mvn -Dmaven.test.skip=true -e clean install mvn clean install mvn clean install -Dmaven.test.skip=true Linux: telnet mq.dev.sum.org  9012 telnet <machine_IP> <port> Sudo su Su - sudo -u dev2adm  -i ps -ef|grep jboss Find a folder Just find directories and skip file names pass the  -type d  option as follows: find   / - type d -name "apt" - ls locate httpdocs Delete latest 3 files: rm -f $(ls -1t ./ | head -3)   working fine ls -d -1tr /path/to/folder/* | head -n -10 | xargs -d '\n' rm -f rm -f $(ls -1t ./ | tail -n +4) netstat -ln | grep '80 ' | grep 'LISTEN' netstat -atnp|grep LISTEN  netstat -ln | grep '99 ' | grep 'LISTEN'  netstat -ln | grep '16'  scp -pr Services-0.0.2-SNAPSHOT.war sbiswas@172.1.8.141:/home/sbiswas tail -f /tmp/Liferay-JBoss.out |grep 'PAGENAME\|tabName' grep 'PAGENAME\|tabName'  for multiple se...

Basic shell scripting part 3

1. What is Shell Scripting ?         Shell scripting, in Linux or Unix,  is programming with the shell using which you can automate your tasks. A shell is the command interpreter which is the interface between the User and the kernel. A shell script allows you to submit a set of commands to the kernel in a batch. In addition, the shell itself is very powerful with many properties on its own, be it for string manipulation or some basic programming stuff.  2. The command "cat file" gives error message "--bash: cat: Command not found". Why?     It is because the PATH variable is corrupt or not set appropriately. And hence the error because the cat command is not available in the directories present PATH variable. 3. How to find the length of a string  in Linux? $ x="welcome" $ echo ${#x} 7 4. What are the different timestamps associated with a file? Modification time:- Refers to the time when the file is last modifie...