Wednesday, July 17, 2013
UniX Exercise 1
- Write a command to print the ouput “Today is Tuesday, 5th day of month, year”.
Sol. date
+”Today is %a, %d of %b, %Y”
- Assume that files in a directory are “filename_dd_mm_yy”, write a command to list the files created today.
Sol. ls
*_`date +”%d_%m_%y”`
- Write a command to copy a file x to y using cat.
Sol. cat
x > y
- Write a command to copy the contents of 2 files into a third file.
Sol. cat
x y > z
- Write a command to copy the contents of x, user input, y into a file.
Sol. cat
x - y > z
- Write a command to copy the contents of x and y prefixing the file name before starting file contents.
Sol. ls x > z; cat x >> z; ls y
>> z; cat y >> z
(Or)
(ls
x; cat x; ls y; cat y) > z
- Write a command to generate the following into a file
Files of the directory
on
Total files in the directory are
Sol.
(echo –e “Files of the directory `pwd` on `date +%D`\n\n”;
ls -l;echo –e “\n\n Total files in the
directory are `ls –l | wc –l`”) > z
- Assume package name of each java program is in line 1, write a command to display all package names.
Sol. head
-1 *.java
- Write a command to display top 3 files which were created recently.
Sol. ls
-ltr | head -4 | tail -3
(Or)
ls -ltr | tail +2 |
head -3
- Write a command to display lines 23 to 44 of file y.
Sol. head
-44 y | tail +23
- Write a command to count number of words in last but one line of file y.
Sol. tail
-2 y | head -1 | wc -w
- Write a command to display 15th line of every file in the current dir.
Sol. cannot
be solved with commands need to write shell script.
- Write a command to display the total number of bytes used by all files of the current directory.
Sol. wc
–c * | tail -1
(or)
cat
* | wc -c
- Write a command to copy file x to y only if y not exits.
Sol ls y || cp x y
- Write a command to convert all characters of file x to capitals.
Sol cat x | tr “a-z” “A-Z”
(or)
tr “a-z” “A-Z” < x
- Write a command to count number of spaces in line 15 of file x.
Sol head
-15 x | tail -1 | tr –cd “ “ | wc -c
- Write a command to convert all tabs to spaces of a file.
Sol cat x | tr “\t” “ “
- Write a command to join all lines of a file.
Sol cat x | tr “\n” “ “
- Write a command to join lines 15 & 24 of a file.
Sol (head
-15 x | tail -1; head -24 x | tail -1) | tr “\n” “ “
(or)
echo
‘head -15 x | tail -1` `head -24 x | tail -1`
- Write a command to display machine hardware type.
Sol uname
–a | cut –d” “ –f6
- Write a command to display file permissions and filename.
Sol ls
-l | tr -s " " | cut -d" " -f1,9
- Write a command to display the file mask applied for file owner.
Sol umask
| cut –c 1
- Write a command to display empno, empname and sal from emp.db.
Sol cut
-d"|" -f1-2,6 emp.db
- Write a command to display the total time taken for the above query.
Sol time
cut -d"|" -f1-2,6 emp.db | tail -3 | head -1
- Write a command to calculate total amount used by company as salaries.
Sol echo `cut -d"|" -f6
emp.db | tail +3 | tr "\n" "+"` 0 | bc
- Write
a command to calculate total amount used by company as salaries along with
the message “Total Salary of all employees is :
”.
Sol echo
-e "Total salary of all employess is
: \c";
echo `cut
-d"|" -f6 emp.db | tail +3 | tr "\n" "+"` 0 | bc
- Write a command to count number of employees in department 10.
Sol cut
-d"|" -f8 emp.db | grep -wc 10
- Write a command to list all employees in the department in which SMITH is working.
Sol cut
-d"|" -f2,8 emp.db | \grep -w
`cut -d"|" -f2,8 emp.db |
grep -w SMITH | cut
-d"|" -f2
write a c shell script that takes an integer number from the keyboard and displays the fibonacci numbners equal to the number entered from the keyboard. thus if the user enters 7, your script displays the first seven fibonacci numbers
#!/bin/bash
#####################################################
# File Name: sy_4_g
#
# Usage: ./sy_4_g
#
# Author: Suresh Yadagiri
# Date: August 06,2010
#
# Requirements:
# write a c shell script that takes an integer number
# from the keyboard and displays the fibonacci numbners
# equal to the number entered from the keyboard.
# thus if the user enters 7, your script displays the
# first seven fibonacci numbers
#
#
#####################################################
function printusageinfo(){
echo -e "\nUsage: $0\n"
}
function is_integer() {
printf "%d" $1 > /dev/null 2>&1
return $?
}
if [ $# -eq 0 ]
then
echo "At least one argument expected"
printusageinfo
exit 1
fi
if [ $# -gt 1 ]
then
echo "Too many arguments"
printusageinfo
exit 1
fi
if ! is_integer $1;
then
echo "Integer argument expected"
printusageinfo
exit 1
fi
if [ $1 -lt 2 ]
then
echo "invalid arguments.enter greater value"
printusageinfo
exit 1
fi
echo "The first $1 fibonacci numbers are"
counter=2
n1=0
n2=1
echo $n1
echo $n2
until [ $counter -eq $1 ]
do
current=`expr $n1 + $n2`
echo $current
counter=`expr $counter + 1`
n1=$n2
n2=$current
done
exit 0
#####################################################
# File Name: sy_4_g
#
# Usage: ./sy_4_g
#
# Author: Suresh Yadagiri
# Date: August 06,2010
#
# Requirements:
# write a c shell script that takes an integer number
# from the keyboard and displays the fibonacci numbners
# equal to the number entered from the keyboard.
# thus if the user enters 7, your script displays the
# first seven fibonacci numbers
#
#
#####################################################
function printusageinfo(){
echo -e "\nUsage: $0
}
function is_integer() {
printf "%d" $1 > /dev/null 2>&1
return $?
}
if [ $# -eq 0 ]
then
echo "At least one argument expected"
printusageinfo
exit 1
fi
if [ $# -gt 1 ]
then
echo "Too many arguments"
printusageinfo
exit 1
fi
if ! is_integer $1;
then
echo "Integer argument expected"
printusageinfo
exit 1
fi
if [ $1 -lt 2 ]
then
echo "invalid arguments.enter greater value"
printusageinfo
exit 1
fi
echo "The first $1 fibonacci numbers are"
counter=2
n1=0
n2=1
echo $n1
echo $n2
until [ $counter -eq $1 ]
do
current=`expr $n1 + $n2`
echo $current
counter=`expr $counter + 1`
n1=$n2
n2=$current
done
exit 0
This script takes integer numbers as command line arguments and displays their sum on the screen if run with no arguments, it informs user with usage message
#!/bin/bash
#######################################################
# File Name : sy_3_g
# Author: : Suresh Yadagiri
# Date : July 27,2010
# Descriptiom :
# This script takes integer numbers as command line
# arguments and displays their sum on the screen
# if run with no arguments, it informs user
# with usage message
#
######################################################
printusageinfo(){
echo "Usage: $0 number-list"
exit 1
}
function is_integer() {
printf "%d" $1 > /dev/null 2>&1
return $?
}
if [ $# = 0 ]
then
echo "Usage: $0 number-list"
exit 1
fi
sum=0 #Running sum initialized to zero
count=0 #To count number of arguments
while [ $# != 0 ]
do
if is_integer $1; then
# "$1 is an integer"
sum=`expr $sum + $1`
count=`expr $count + 1`
shift # Shift the counted number out
else
echo "$1 is not an integer"
printusageinfo
fi
done
#displaying final sum
echo "The sum of given $count numbers is $sum."
exit 0
#######################################################
# File Name : sy_3_g
# Author: : Suresh Yadagiri
# Date : July 27,2010
# Descriptiom :
# This script takes integer numbers as command line
# arguments and displays their sum on the screen
# if run with no arguments, it informs user
# with usage message
#
######################################################
printusageinfo(){
echo "Usage: $0 number-list"
exit 1
}
function is_integer() {
printf "%d" $1 > /dev/null 2>&1
return $?
}
if [ $# = 0 ]
then
echo "Usage: $0 number-list"
exit 1
fi
sum=0 #Running sum initialized to zero
count=0 #To count number of arguments
while [ $# != 0 ]
do
if is_integer $1; then
# "$1 is an integer"
sum=`expr $sum + $1`
count=`expr $count + 1`
shift # Shift the counted number out
else
echo "$1 is not an integer"
printusageinfo
fi
done
#displaying final sum
echo "The sum of given $count numbers is $sum."
exit 0
write a Bourne shell script that takes a file name and a directory name as command line arguments and removes the file if it is found under the given directory and is a simple file. If the file(the first argument) is a directory, it is removed (including all the files and subdirectories under it).
#!/bin/bash
#########################################################
# File Name : sy_2_g
# Author: : Suresh Yadagiri
# Date : August 10,2010
# Descriptiom :
# write a Bourne shell script that takes a file name and
# a directory name as command line arguments and removes
# the file if it is found under the given directory and
# is a simple file. If the file(the first argument) is a
# directory, it is removed (including all the files and
# subdirectories under it).
#
#########################################################
function printusage(){
echo -e "\nusage:\n$0 filename directoryname"
echo " or "
echo -e "$0 directoryname directoryname"
exit 1
}
if (( $# < 2 || $# > 2 ))
then
echo "Invalid arguments"
printusage
fi
if [ ! -d $2 ] ; then
echo " $2 is not a directory or exists"
printusage
fi
if [ ! -d "$2/$1" ] && [ ! -f "$2/$1" ]
then
echo "\"$1\" is not a directory or a file inside \"$2\"."
printusage
fi
if [ -d "$2/$1" ] ; then
echo "\"$1\" directory removed from $2"
rm -r "$2/$1"
else
echo "\"$1\" file removed from $2"
rm -f "$2/$1"
fi
#########################################################
# File Name : sy_2_g
# Author: : Suresh Yadagiri
# Date : August 10,2010
# Descriptiom :
# write a Bourne shell script that takes a file name and
# a directory name as command line arguments and removes
# the file if it is found under the given directory and
# is a simple file. If the file(the first argument) is a
# directory, it is removed (including all the files and
# subdirectories under it).
#
#########################################################
function printusage(){
echo -e "\nusage:\n$0 filename directoryname"
echo " or "
echo -e "$0 directoryname directoryname"
exit 1
}
if (( $# < 2 || $# > 2 ))
then
echo "Invalid arguments"
printusage
fi
if [ ! -d $2 ] ; then
echo " $2 is not a directory or exists"
printusage
fi
if [ ! -d "$2/$1" ] && [ ! -f "$2/$1" ]
then
echo "\"$1\" is not a directory or a file inside \"$2\"."
printusage
fi
if [ -d "$2/$1" ] ; then
echo "\"$1\" directory removed from $2"
rm -r "$2/$1"
else
echo "\"$1\" file removed from $2"
rm -f "$2/$1"
fi
Subscribe to:
Posts (Atom)