Tuesday, 7 February 2017

Shell scripting interview questions



What is the output if you execute ./temp.sh 1 2 3 4 5 6 7 8 9 20

Temp.sh

#!/bin/bash
echo $10

10


If we have profile file as given below what is the contents of  /tmp/log.txt in these two cases :
cat profile 2>&1  1>/tmp/log.txt
and
cat profile 1>/tmp/log.txt 2>&1

profile

PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin
PATH=$PATH:/opt/freeware/bin:/system/products/openssh/bin:/system/products/sudo/bin:$HOME/bin:.

export PATH


Soft link vs. Hard link



If /tmp/details.txt have read and write  permission to user1.
user1 rights one application by which other  user’s execute it and put their name and address in details.txt file.

How you do it ?




If i execute command "ls" then system display's an error command not found.
But when i do "find -name ls" then it is in /usr/bin/ls.
Why is then this error comes and what is the solution for it ?



There is file in /tmp directory with name test.txt of user USER1 and have the permission 777.
When user USER2 tries to delete that file , he won't able to delete it.
Why ?



What is the output of :
A=321; (A=123); echo $A



If we do not have profile file then what is the contents of /tmp/log.txt in these two cases : 
cat profile  2>&1  1>/tmp/log.txt 
cat profile  1>/tmp/log.txt 2>&1




If I execute temp.sh it is giving an error that not able to execute /var/temp1.sh  may be permission denied.
But if we see the permissions on both files, 777 is there.
Steps to execute it
cd /usr/
./temp.sh


/usr/temp.sh                              
#!/bin/bash
./temp1.sh
echo Hello
 

/var/temp1.sh
#!/bin/bash 
exit 1

Write a one line script using awk  to reverse every string of given line.
awk '{for (i=1;i<=NF;i++){ len=length($i); for(j=len;j>0;j--) { temp=substr($i,j,1); printf("%c",temp)} printf(" ")} printf("\n")}'