LINUX and Shell Programming
Q-1 (A) Do as directed.
1) How do you get the value of 10th positional parameter ?
Ans : We can use shift command to get the value of 10th positional parameter.
2) What will be the output of following code snippet?Assume that file LINUX.txt having 7 lines.
while read n
do
echo '$n'
done < LINUX.txt
Ans :
$n
$n
$n
$n
$n
$n
$n
3) Suppose you are executing a command and that command is having an error. How do you get that command is successfully executed or not?
Ans : we can use "$?". If previous command can successfully run that time $? will return value 0 other wise 1.
4) Why shell is known as command interpreter? Write an absolute path for bash file.
Ans : path : #!/bin/bash
Q-1 (B) Answer the following in brief.
1) What information is contain by $# variable? Write a shell script that display command line arguments one in each line.
Ans : $# is contain total number of argument.
Ex :
for i in $*
do
echo $i
done
2) what is the output of following code segment?Why?Assume that you are passing three command line arguments.
for i in $*
do
echo $i
done
echo "========="
for i in "$*"
do
echo $i
done
Ans :
1
2
3
=========
1 2 3
3) Write a difference between absolute path and relative path using example.
Ans :
Absolute path :
- Start at the root directory ( / ) and work down.
- Put a slash ( / ) after every directory name - though if the path ends at a directory, the slash after the last name is optional.
- Never start with a slash.
- Ex - > /var/ftp/pub
/etc/samba.smb.conf
/boot/grub/grub.conf
Relative path :
- Start at your current directory
- Put a slash ( / ) after every directory name - though if the path is to a directory, the slash after the last name is optional.
- Always start with a slash ( / ).
- Ex - > rm read/hello.txt
4) List any six file operators. Write a code snippet that check the directory file.
Ans :
-d, -e, -f, -e, -z, -x, -wA) Do as directed.
1) Write a shell script that take a file name as input from user and check that file is exists or not and if exists then check file is directory or regular file.
Ans :
#!/bin/bash
echo -n "Enter File name"
read a
if [ -d $a ]
then
echo "This is directory.."
elif
then
echo "this is file"
else
echo "this file is not exist"
fi
2) Write a script program that accept a number from command line and count number of odd and even digits within given number. Do appropriate command line validations.
Ex. if user input number 1234567 then the output odd digits are 4 and even digits 3.
Ans :
#!/bin/bash
#Write a script to count number of odd and even digits within given number.
echo "Enter Number : "
read a
tmp=$a
r=0
odd=0
even=0
while [ $tmp -ne 0 ]
do
r=`expr $r % 10`
tmp=`expr $tmp / 10`
if [ $r -eq 0 ]
then
echo "Number is Zero"
elif [ `expr $r % 2 ` -eq 0 ]
then
even=`expr $v + 1`
else
odd=`expr $s + 1`
fi
done
echo "Toatal Even Number is :-> $even"
echo "Total ODD Number is :-> $odd"
OR
A) Do as directed.
1) Write shell script that print total number of regular files and total number of directory files from current directory.(Do not use GREP and WC command.)
Ans :
# !/bin/bashAns :
# Write a script to count total number of directories and files under the current directory.
ls > temp
c=0
count=0
while read a
do
if [ -f $a ]
then
c=`expr $c + 1 `
fi
if [ -d $a ]
then
count=`expr $count + 1 `
fi
done < temp
echo "Total file is = $c"
echo "Total directory is = $count"
2) Write a script which takes a number as input from command line and display it in word form as given below.Put appropriate command line validations.
input : 56
OutPut : Five Six
input : 3490
OutPut : Three Four Nine Zero
Ans :
# !/bin/bash
num=$1
temp=$num
if [ $# -eq 1 ]
then
while [ ! $temp -eq 0 ]
do
rem=`expr $temp % 10`
temp=`expr $temp / 10`
if [ $rem -eq 0 ]
then
echo -n "Zero "
elif [ $rem -eq 1 ]
then
echo -n "One "
elif [ $rem -eq 2 ]
then
echo -n "Two "
elif [ $rem -eq 3 ]
then
echo -n "Three "
elif [ $rem -eq 4 ]
then
echo -n "Four "
elif [ $rem -eq 5 ]
then
echo -n "Five "
elif [ $rem -eq 6 ]
then
echo -n "Six "
elif [ $rem -eq 7 ]
then
echo -n "Seven "
elif [ $rem -eq 8 ]
then
echo -n "Eight "
elif [ $rem -eq 9 ]
then
echo -n "Nine "
fi
done
else
echo "Please Enter Only One Argument."
fi
B) Do as directed.
1) Convert the following permission code to octal.
a)u=rwx,g=x,o=x
Ans : 711
b)u-rx,g+w,o-x
Ans : 521
c)=read
Ans : 444
d)a+rw
Ans : 666
e)u-x,g+w,o-r
Ans : 623
2) Convert the following permission rwx triplets.
a)110
Ans : --x--x---
b)505
Ans : r-x---r-x
c)777
Ans : rwxrwxrwx
d)15
Ans : -----xr-x
e)101
Ans : --x-----x
OR
1) Write a command to move which display calendar of January month of 2025.
Ans :$ cal 1 2025
2) write a command that display time in hour:minute:second format.Like 05:20:45.
Ans :$ date +"%T"
3) Write a command which set full permissions at all the level while you create new directory.
Ans :$ umask 000
4) Write a command To apply following permissions on file LINUX.txt .Use octal code.
Ans :
a)r-xrw-rwx
$ chmod 567 LINUX.txt
b)rw-r-x--x
$ chmod 651 LINUX.txt
5) Write a single command To Move in to the directory. "Demo" which is located in "Desktop" directory.Your current directory is "home"."Desktop" directory is located in "home" directory.
Ans : $ ./Desktop/Demo
Q-3 Answer the following in detail
A) What are three levels of security in UNIX? List type of files available in UNIX.Explain four blocks in file system implements in UNIX.
Ans :
Three levels of security in UNIX
1) System
2) File
3) Directory
In UNIX seven type of file
1) Regular file
2) Directory file
3) Block Special file
4) Character Special file
5) Socket file
6) FIFO file
7) Symbolic link file
In UNIX Four Blocks in file system implements in UNIX.
1) Boot Block
2) Super Block
3) Inode Block
4) Data Block
Boot Block: located in the first few sectors of a file system. The boot block contains the initial bootstrap program used to load the operating system. Typically, the first sector contains a bootstrap program that reads in a larger bootstrap program from the next few sectors, and so forth.
Super Block: Each filesystem has one super block (+ duplicate super block) it contains info about
1. type of filesystem (ext2, ext3...)
2. the block size
3. pointers to a list of free blocks
4. the inode number of the root directory
5. magic number
iNode: Every file have one inode and vice versa. In linux every file is recognized with integer number known as inode number. This structure consists of info of file about
1. file ownership indication
2. file type (e.g., regular, directory, special device, pipes, etc.)
3. file access permissions. May have setuid (sticky) bit set.
4. time of last access, and modification
5. number of links (aliases) to the file
6. pointers to the data blocks for the file
7. size of the file in bytes (for regular files), major and minor device numbers for special devices.
Data Block : It contains actual data of file for all 7 types.
Data Block : It contains actual data of file for all 7 types.
B) User execute a command ls -l a* . How shell interpret the command?Explain the steps in detail with given command example.
Ans :
Write Command Execution Process.
C) Write an absolute path for the file which stores information about user including user name and password.How UNIX provide a security on password though it is available publicly to all user?List and describe the columns of that file.
- Username: It is used when user logs in. It should be between 1 and 32 characters in length.
- Password: An x character indicates that encrypted password is stored in /etc/shadow file.
- User ID (UID): Each user must be assigned a user ID (UID). UID 0 (zero) is reserved for root and UIDs 1-99 are reserved for other predefined accounts. Further UID 100-999 are reserved by system for administrative and system accounts/groups.
- Group ID (GID): The primary group ID (stored in /etc/group file)
- User ID Info: The comment field. It allow you to add extra information about the users such as user’s full name, phone number etc. This field use by finger command.
- Home directory: The absolute path to the directory the user will be in when they log in. If this directory does not exists then users directory becomes /
- Command/shell: The absolute path of a command or shell (/bin/bash). Typically, this is a shell. Please note that it does not have to be a shell.

No comments:
Post a Comment