0% found this document useful (0 votes)
61 views25 pages

Rishita Os 2 1

Uploaded by

parteek singhal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views25 pages

Rishita Os 2 1

Uploaded by

parteek singhal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

ASSIGNMENT-2

Permission Commands
1. Permission Types
o Owner permissions − The owner's permissions determine what actions the
owner of the file can perform on the file.
o Group permissions − The group's permissions determine what actions a user,
who is a member of the group that a file belongs to, can perform on the file.
o Other (world) permissions − The permissions for others indicate what action
all other users can perform on the file.

2. Examining Permissions

ls -l abc.txt

 The first three characters (2-4) represent the permissions for the file's owner.
 The second group of three characters (5-7) consists of the permissions for the group
to which the file belongs.
 The last group of three characters (8-10) represents the permissions for everyone
else.

3. Changing Permissions (Symbolic Method Numeric Method)

Numerical

chmod 476 abc.txt

Symbolic

chmod -rwx abc.txt


4. Default Permissions And Umask

umask

umask 0222

File

1. Obtain a complete list of all files & directories in the whole system & save the output
in a file?

ls -a > output.file
2. Create a file new_file. Assign all permissions to the owner and remove all permissions
from others using Relative assignment and Absolute assignment

Relative assignment
cat > new_file.txt
chmod u+rwx new_file.txt

Absolute assignment
cat > new_file1.txt
chmod 700 new_file.txt

3. Assuming that a file’s current permissions are “rw-r-xr—“, to specify the chmod
expression required to change them to:
i) r w x r w x r w x
[chmod 777 [filename]] in absolute and [chmod a=rwx [filename]] in relative.

ii) r - - r - - - - -
[chmod 440 [filename]] in absolute and [chmod ug=r, o-rwx [filename]] in relative.
iii) - - r - -r - -
[chmod 044 [filename]] in absolute and [chmod u-rwx [filename], go=r [filename]] in
relative.
v) - - - - - - - - -
[chmod 000 [filename] in absolute and [chmod ugo-rwx [filename]] in relative
4.Write a shell script which receives two filenames as argument it should check whether
the file content are same or not. if they are same then 2nd file should be deleted.

echo "enter the first file name"


read file1
echo "enter the second file name"
read file2
cmp $file1 $file2 && rm $file2
if [ -e $file1 ]
then
if [ ! -e $file2 ]
then
echo " same "
else
echo " diff"
fi
else echo "$file1 is not existed"
fi

5. Find out whether the specified file is ordinary directory or file?

PASSED=$1
[ -d "$PASSED" ] && echo "directory"
[ -f "$PASSED" ] && echo "file"

6.Find out whether the specified file grants read, write or execute permission

echo "enter filename"


read name
if [ -f $name ]
then
if [ -r $name ]
then
echo "read permission"
fi
if [ -w $name ]
then
echo "write permission"
fi
if [ -x $name ]
then
echo "execute permission"
fi
else
echo "Could not find"
fi

7.Find out whether the file exists or not, if not then create a file
echo "enter filename"
read file
if [ ! -f $file ]
then
echo "No file named $file exists"
touch $file
echo "$file has now been created"
ls -l $file
else
echo "$file exists"
fi
8. Find out whether a file contains any data or not if it contains then display the content
of file
echo "Enter filename: "
read file
if [ -s $file ]
then
echo "$file contains some data which is displayed below"
cat $file
else
echo "$file is does not contain any data"
fi

9. To display the top & last lines of user made file.

10. To copy the contents of one file to another file in a separate directory
11. Write a shell script to display the size of files from current working directory

echo "size of all files and directories"


du -h -all
12. To accept a filename as command line argument and print its contents
echo "Contents of $1 : "
cat $1

Process related commands

1. ps: ps command is used to list the currently running processes and their PIDs along with
some other information depends on different options
2. top: top command is used to show the Linux processes. It provides a dynamic real-time
view of the running system. Usually, this command shows the summary information of the
system and the list of processes or threads which are currently managed by the Linux Kernel

3. Last :Each time a user logs into the system, a record for that session is written to
the /var/log/wtmp file. last reads the file wtmp file and prints information about the logins
and logouts of the users. Records are printed in reverse time order, starting from the most
recent ones.

4. pstree:pstree is a Linux command that shows the running processes as a tree.


5. Kill : used to terminate a running process manually

6. nice, renice,fg, bg

7. Write a shell script which takes a name as parameter and returns the PID(s) of
processes with that name.

processId=$(ps -ef | grep 'ps' | grep -v 'grep' | awk '{ printf $2 }' )
echo $processId

Environment variables and Local variables


Disk Usage and /Miscellaneous Commands and use of pipes

1. df:Linux df command is used to display the disk space used in the file system.
-h, --human-readable: It is used to display sizes in powers of 1024 (e.g., 1023M).

2.du : du command, short for disk usage, is used to estimate file space usage.

3. head :The head command, as the name implies, print the top N number of data of the
given input
tail :The tail command, as the name implies, print the last N number of data of the given
input.

4. spell:the spell command is a spell-checking program which scans a text file for misspelled


words, and prints each misspelled word on its own line.
Shell Scripts- Variables- Environmental, global and Local;Conditions- If,
if----else

1.To find the largest of two numbers.

echo "enter num1"


read num1
echo "enter num2"
read num2
if [ $num1 -gt $num2 ]
then
echo $num1
else
echo $num2
fi

2.To check whether a number is even or odd


echo "enter a num:"
read num
if [ $((num%2)) -eq 0 ]
then
echo "number is even"
else
echo "number is odd"
fi
3. To print the values of Environment variables
echo $BASH
echo $BASH_HOME
echo $PATH
4. Write a shell script to determine whether the given string is a palindrome or not.

echo "enter string"


read name
echo ""
name1= echo $name | rev
if [[ "$name" -eq "$name1" ]]
then
echo "palindrome"
else
echo "not palindrome"
fi

Shell Scripts- nested if, Case-------esac


1. To find largest of three given number.
echo "Enter first number"
read a
echo "Enter second number"
read b
echo "Enter third number"
read c
if [ $a -gt $b -a $a -gt $c ]
then
echo $a "is greatest"
elif [ $b -gt $c ]
then
echo $b "is greatest"
else echo $c "is greatest"
fi
2. To greet the user according to the week of the day.
week= " $(date +'%A')"
echo "today is $week"

3. To display the number of vowels and consonants in a string


echo –n “Enter String:-”
read string
vowcount=$(echo $string | grep –o –i “[aeiou]” | wc –l)
conscount=$(echo $string | grep –o –i “[bcdfghjklmnpqrstvwxyz]” | -l)
echo “the given string has $vowcount vowels and $conscount consonants”
4. Write a script to input two strings and compare them,whether they are equal. Also
print the string which is greater (which comes later in the dictionary)
echo "enter string1"
read string1
echo "enter string2"
read string2
if [ "$string1" == "$string2" ]
then
echo "equal"
elif [ "$string1" > "$string2" ]
then
echo "$string1 is greater"
else
echo "$string2 is greater"
fi
5. Write a shell script to reverse a string.
read s
x=`expr $s|wc -m`
y=`expr $x-1`
r=`echo "$s"|cut -c $x`
for((i=$x-1;i>=0;i--))
do
c=`echo "$s"|cut -c $i`
r=$r$c
done
echo " $r"

Shell Script count


1. Write a script to accept a file name from user and count the number of words in it.

echo "Enter file name"


read f
words=’cat $f |wc -w`
lines=`cat $f |wc -l`
chars=`cat $f |wc -m`
echo "No of Lines =" $lines
echo "No of words =" $words
echo "No of characters =" $chars

2. Write a shell script to count the number of words enclosed between $ and # symbols.
3. Write a script to search for the word ‘exam’ in a file and display total no. of matching
words.
grep -o -i exam file2.txt | wc -l

Array

1. Write a script to concatenate two arrays


read a
read b
a+=$b
echo $a
2. Write a script to enter the names of some countries in the array and replace USA
with United States of America.
read arr
list=(${arr})
echo ${list[@]/USA/Unites States of America}

3. Write a script to enter some numbers in the array and print the sum of all the
numbers.
echo "enter num"
read num
sum=0
while [ $num -gt 0 ]
do
mod=$((num % 10))
sum=$((sum + mod))
num=$((num / 10))
done
echo $sum
4. Write a script to load an array with some contents, display them, unset the entire
array and load the new contents.

unix=('khyati' 'megha' 'gaurav' 'hemant')


shell=('manya' 'kush' 'drishti')
unixshell=("${unix[@]}" "${shell[@]}")
echo ${#unixshell[@]}
unset unixshell
echo ${#unixshell[@]}

5. Write a script to search an item in the array.

echo "enter number of elements"


read n
echo "enter array"
read all
echo "element to be searched"
read element
FOUND="NOTFOUND"
for e in $all
do
if [ $element == $e ];
then
echo "sucess"
FOUND="FOUND";
break;
fi
done
if [ $FOUND == "NOTFOUND" ];
then
echo "not Found"
fi
Shell Script Variables
1. Write script to see current date, time, username and current directory
echo "hello, $LOGNAME"
echo "Current date is "
date
echo "User is"
whoami
echo "Current directory"
pwd
2. Write a script to great user according to time of the day
a. Good morning 4 am-12 noon
b. Good afternoon 12 noon -5 pm
c. Good evening 5 pm -8 pm
d. Good night 8 pm – 4 am

h='date +%H'
if [ $h -lt 12 ]; then
echo "good morning"
elif [ $h -lt 17 ]; then
echo "good afternoon"
else
echo "good night"
fi

Shell Script Operators

1.Write a script to perform real number calculation and store result to third variable,
let’s say a=5.66, b=4.5, c=a+b

echo “Enter 2 real numbers”


read a
read b
c=`echo $a + $b | bc`
echo "$a + $b = $c"

2.The marks obtained by student in five different subject are input through A keyboard
the student gets a division as per following rules:-
a. Percentage above or equal to 60% - 1st division
b. Percentage between 50 % & 59% - 2nd division
c. Percentage between 40 % & 49% - 3rd division
d. Less then 40% fail
echo "Enter marks of two subjects"
read m1
read m2
per=`echo \($m1 + $m2 \) / 2 | bc`
echo
echo "Percentage is $per"
if [ $per -ge 60]
then
echo "First division"
elif [$per -ge 50 -a $per -lt 60]
then
echo "Second division"
elif [$per -ge 40 -a $per -lt 50]
then
echo "Thid division"
else
echo "Fail"
fi

3.In a company an employee is paid as under –if its salary is <20,000 then HRA =10%
of basic salary and DA=90% of basic salary. If salary equal to or above 20,000 then
HRA =500 and DA = 98% of basic salary. If employee salary is input through keyboard,
write a shell script to find the gross salary.

echo "enter basic sal:"


read bsal
if [ $bsal -lt 20000 ]
then gsal=$((bsal+((bsal/100)*10)+((bsal/1100)*90)))
echo "gross salary is : $gsal"
fi
if [ $bsal -ge 20000 ]
then gsal=$((bsal+500)+((bsal/100)*98)))
echo "gross sal is : $gsal"
fi
4.If CP and SP of an item is input through the keyboard.Write a shell script to
determine whether the seller has made profit or loss. Also determine profit and loss
percentage.

echo "cost price"


read cp
echo "selling price"
read sp
if [ $cp -eq $sp ]
then
echo "no profit or no gain"

fi
if [ $cp -gt $sp ]
then
s=$((cp - sp))
echo "loss $s"
else
s=$((sp - cp))
echo "profit $s"
fi

You might also like