1. Check the output of the following commands.....
Shared By : Divya S Didwania (LCIT) Show/Hide Program
******************************************************************
1.Check the output of the following commands.
mkdir and rmdir
******************************************************************
[Divya@linux ~]$ mkdir family
[Divya@linux ~]$ cd family
[Divya@linux family]$ mkdir grpr
[Divya@linux family]$ rmdir grpr
[Divya@linux family]$ mkdir parent
[Divya@linux parent]$ mkdir child
[Divya@linux parent]$ cd child
[Divya@linux child]$ cat c1
it is first fle
^z
[Divya@linux child]$ls
c1
[Divya@linux child]$cd ../
[Divya@linux family]$ cd ../..
[Divya@linux home]$
Shared By : Your Name (College - Place ) Show/Hide Program
Share this program...
Send it to gtumca@gmail.com
with your Name - College name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
2. Write shell script...
Shared By : Divya S Didwania (LCIT) Show/Hide Program
2.
******************************************************************
2.2 Accept the string and checks whether the string is palindrome or not.
******************************************************************
echo "enter the string"
read a
b=`echo $a|rev`
#c=`echo $a -eq $b`
if [ "$a" == "$b" ] ; then
echo "pali "
else
echo "not pla"
fi
[ketan@linux ~]$ sh k2.sh
enter the string
mom
pali
[ketan@linux ~]$ sh k2.sh
enter the string
divya
not pla
******************************************************************
2.3 Accept number and check the number is even or odd, finds the length of the number, sum of the digits
in the number.
******************************************************************
echo "Enter the number"
read n
if [ $[$n % 2] -eq 0 ]
then
echo " $n is even"
else
echo " $n is odd "
fi
******************************************************************
or
******************************************************************
2.3 Accept number and check the number is even or odd, finds the length of the number, sum of the digits
in the number
******************************************************************
echo "Enter the Number"
read a
z=$a
cont=0
sum=0
sd=0
while [ $a -gt 0 ]
do
sd=`expr $a % 10`
cont=`expr $cont + 1`
sum=`expr $sum + $sd`
a=`expr $a / 10`
done
echo "Sum of digit for numner is $sum"
echo " Total length $cont"
b=`expr $z % 2`
if test $b -eq 0
then
echo "Even number"
else
echo "Odd Number"
fi
******************************************************************
2.4
Accept strings and replace a string by another string.
******************************************************************
echo enter string
read stringname
echo enter replacable string
read replacename
echo enter file name
read filename
sed s/$stringname/$replacename/g $filename > temp
~
[divya@linux ~]$ sh 4.sh
enter string
payal
enter replacable string
pallavi
enter file name
s
[divya@linux ~]$ cat temp
roll no name marks
1 vaishali 50
2 viraj 40
3 sonal 50
4 pallavi 30-------------
5 nehal 40
6 pallavi 60--------------
[divya@linux ~]$ cat s
roll no name marks
1 vaishali 50
2 viraj 40
3 sonal 50
4 payal 30
5 nehal 40
6 payal 60
************************************************************************************************************************************
2.5 Accept filename and displays last modification time if file exists, otherwise display appropriate
message.
************************************************************************************************************************************
echo "Enter the file name"
read a
z=`pwd $a`
if
[ -d $z/$a ]
then
b=`ls -lt $a`
echo $b
elif
[ -f $z/$a ]
then
q=`ls -lt $a`
echo $q
else
echo " no such file or directory exist"
fi
"5.sh" 19L, 204C written
[divya@linux ~]$ ls
1 3.sh 6.sh d2 exist.sh first s temp2
2.sh 5.sh d1 d3 f login.sh temp1
[divya@linux ~]$ sh 5.sh
Enter the file name
first
-rw-rw-r-- 1 divya divya 182 2010-07-31 14:58 first
[divya@linux ~]$ sh 5.sh
Enter the file name
d1
total 8 -rw-rw-r-- 1 divya divya 4 2010-07-30 13:38 first
[divya@linux ~]$ sh 5.sh
Enter the file name
d4
no such file or directory exist
******************************************************************
2.6 Fetch the data from a file and display data into another file in reverse order.
******************************************************************
echo "Enter the file name"
read a
b=`cat $a`
echo $b
`sort -r $a > first `
"2.sh" 8L, 80C written
[divya@linux ~]$ cat first
[divya@linux ~]$ cat s
roll no name marka
1 vaishali 50
2 viraj 40
3 sonal 50
4 payal 30
5 nehal 40
[divya@linux ~]$ sh 2.sh
Enter the file name
s
roll no name marka
1 vaishali 50
2 viraj 40
3 sonal 50
4 payal 30
5 nehal 40
[divya@linux ~]$ cat first
roll no name marka
5 nehal 40
4 payal 30
3 sonal 50
2 viraj 40
1 vaishali 50
******************************************************************
Shared By : diz Show/Hide Program
#2.a) Accept numbers and perform addition, subtraction, division and multiplication.
a=10
echo "Enter The Value of A: "
read a
b=10
echo "Enter The Value of B: "
read b
c=0
c=`expr $a + $b`
echo "Addition " $c
c=`expr $a - $b`
echo "Subtraction " $c
c=`expr $a / $b`
echo "Division " $c
c=`expr $a \* $b`
echo "Multiplication " $c
#b) Accept the string and checks whether the string is palindrome or not.
l=0
cnt=1
tag=0
echo "Enter a String?"
read str
l=`echo $str |wc -c`
l=`expr $l - 1`
lh=`expr $l / 2`
while [ $cnt -le $lh ]
do
c1=`echo $str|cut -c$cnt`
c2=`echo $str|cut -c$l`
if [ $c1 != $c2 ]
then
tag=1
fi
cnt=`expr $cnt + 1`
l=`expr $l - 1`
done
if [ $tag -eq 0 ]
then
echo "String is Palindrome"
else
echo "String is not Palindrome"
fi
#c.Accept number and check the number is even or odd, finds the length of the number, sum of the digits
in the number.
echo "Enter number:"
read no
c=`expr $no % 2`
if [ $c -eq 0 ]
then
echo "No is even "
else
echo "No is odd"
fi
cnt=`echo $no | wc -c`
cnt=`expr $cnt - 1`
echo "word count =" $cnt
i=1
while [ $i -le $cnt ]
do
j=`echo $no | cut -c $i`
k=`expr $k + $j`
i=`expr $i + 1`
done
echo "total =" $k
#d.Accept strings and replace a string by another string.
echo "Enter any string:"
read str
echo "Your string is :" $str
echo "Enter word that your what replace with other word"
read oldstr
echo "Enter new string"
read newstr
str1=`echo $str | sed s/$oldstr/$newstr/g`
str2=`echo $str | replace $oldstr $newstr`
echo "after replacing " $str1
echo "after replacing " $str2
#e.Accept filename and displays last modification time if file exists, otherwise display appropriate
message.
echo "Enter file name"
read fnm
if [ ! -f $fnm ]
then
echo "File Not Found"
exit 1
fi
dt=`date -r $fnm`
echo "modification date is:" $dt
#f.Fetch the data from a file and display data into another file in reverse order.
echo "enter file name for fatching data and store in other file"
read fnm
if [ ! -f $fnm ]
then
echo "File not found"
exit 1
fi
echo "enter new file name:"
read newfile
while read -n1 char;
do
str=$char$str
done<$fnm
echo $str>$newfile
echo $newfile "successfully created on" `pwd` " path"
3. Write a script to find the global complete path for any file....
#!/usr/bin/env bash
# GTU3: Write a script to find the global complete path for any file.
# Code written By: Rushyang Darji
# Last Updated: 19.10.2010
# Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts.
echo -e "Enter the filename to search in current directory: \c"
read FILE
args=`find . -name $FILE | xargs` # xargs builds arguments from find, for using in "for loop"... Remember, you should never Parse result of "ls" in any case because unix allows every character to be used in naming files, even if a "new line character"... execute "touch $'A\nFile'" to make file and ls to know it. Google "parsing output of ls" to know more.
for i in $args
do
if [ -f "$i" ]; then
CPATH=`readlink -f "$i"` # readlink returns the symbolic link, -f canonicalize by every parent directory recursively.
echo $CPATH
fi
done
noargs=${#args} # noargs stores total number of arguments.
if [ "$noargs" -eq "0" ]; then
echo "No such a file exists"
fi
Shared By : Divya S Didwania (LCIT) Show/Hide Program
3.Write a script to find the global complete path for any file.
******************************************************************
echo "enter the filename"
read a
b=`pwd $a`
if
[ -f $b/$a ]
then
echo "file exist"
echo $b
else
echo "file does not exist"
fi
~
~
"3.sh" 11L, 131C written
[divya@linux ~]$ sh 3.sh
Enter the filename
f
file exist
/home/divya
[divya@linux ~]$ sh 3.sh
Enter the filename
d1
file does not exist
or
echo "enter file name to find:"
read fnm
ans=0;
if [ ! -f $fnm ]
then
if [ ! -d $fnm ]
then
echo "File not found"
exit 1
fi
fi
ans=`find /home -name $fnm `
echo $ans
******************************************************************
Shared By : diz Show/Hide Program
#3.3. Write a script to find the global complete path for any file.
echo "enter file name to find:"
read fnm
ans=0;
if [ ! -f $fnm ]
then
if [ ! -d $fnm ]
then
echo "File not found"
exit 1
fi
fi
ans=`find /home -name $fnm `
echo $ans
4. Write a script to broadcast a message to a specified user or a group...
Shared By : Divya S Didwania (LCIT) Show/Hide Program
4. Program :- message Broadcast for a Single User,Multiple Users and to All
******************************************************************
who > e.txt
echo "For Broadcasting Messages."
if [ $# = 0 ]
then
echo "Enter the User Name:\c"
read nam
p=`grep $nam e.txt`
if [ "$p" != "" ]
then
write $nam
rm e.txt
exit
else
echo "The user is not logged in"
fi
rm e.txt
exit
fi
if [ "$1" = "all" ]
then
wall
fi
for nam in $*
do
p=`grep $nam e.txt`
if [ "$p" != "" ]
then
write $nam
else
echo "The User $nam is not logged in"
fi
done
output
[Divya@linux ~]$ sh 4.sh
For Broadcasting Messages.
Enter the User Name:\c
bhoomika
hellow to u
[8]+ Stopped sh 4.sh
[Divya@linux ~]$ sh 4.sh
For Broadcasting Messages.
Enter the User Name:\c
ketan
The user is not logged in
[Divya@linux ~]$
******************************************************************
Shared By : Hardikgiri Goswami (Indus Institiute ) Show/Hide Program
echo "Enter the User name :"
read uname
echo "Enter String u want to send (End with Control-D) :"
write $uname
echo -e "\nYour message send successfully...."
5. Write a script to copy the file system from two directories to a...
#!/usr/bin/env bash
# GTU 5 - Write a script to copy the file system from two directories to a new directory in such a way that only the latest file is copied in case there are common files in both the directories.
# Code written By: Rushyang Darji
# Last Build: 24.08.2010
# Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts.
EXIT=n
while [ $EXIT != y ]
do
sleep 1
echo -e "\n"
echo -e " 1. Display PWD
2. Long Listing
3. Change Directory
4. Copy Newest File.
5. Exit
Enter Choice: \c"
read ch
case $ch in
1)
clear
pwd
;;
2)
clear
pwd
ls -l
;;
3)
echo -n "Enter Absolute Path to change directory: "
read apath
cd $apath
if [ $? -eq 0 ]; then # We can also check for availibility of directory before 'cd' command by 'test -d $apath' i.e. 'if [ -d $apath ]'
clear
echo "Working Directory Changed successfully to.."
sleep 1
pwd
else
clear
echo "Please check your PATH."
fi
;;
4)
clear
echo "Enter filenames to copy. ( * - for ALL Files, ELSE Separate files by spaces )"
read allfiles
if [ -f $allfiles ]; then
echo "Enter Absolute path, where to copy these files: "
read -e cpath
if [ -d $cpath ]; then
cp -u "$allfiles" $cpath # -u copies only when the SOURCE file is newer than the destination file or when the destination file is missing
else
echo "There is no such a directory!"
fi
else
echo "There is/are no such file(s)!"
fi
;;
5)
clear
echo -n "Exiting.."
sleep 1
echo -n "."
sleep 1
echo -n "."
clear
exit
;;
*)
clear
echo "Invalid Choice"
;;
esac
done
Shared By : Your Name (College - Place ) Show/Hide Program
Share this program...
Send it to gtumca@gmail.com
with your Name - College name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
6. Write a script to compare identically named files in two different...
#!/usr/bin/env bash
# Code written By: Rushyang Darji
# Last Build: 09.10.2010
# Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts.
while true; do
read -e -p "Enter first Directory's Absolute path: " path1 || exit
[[ -d $path1 ]] && break
echo "Invalid path, Try Again!"
done
while true; do
read -e -p "Enter second Directory's Absolute path: " path2 || exit
[[ -d $path2 ]] && break
echo "Invalid path, Try Again!"
done
while true; do
read -e -p "Enter Third Directory's Path, to copy files in case of exact match: " path3 || exit
[[ -d $path3 ]] && break
echo "Invalid Path, Try, Again!"
done
temp=$(mktemp)
for i in $path1/*
do
if [ -f "$i" ]; then
for j in $path2/*
do
if [ -f "$j" ]; then
base1=`basename "$i"`
base2=`basename "$j"`
if [ "$base1" = "$base2" ]; then
diff "$i" "$j" > $temp
size=`ls -s $temp | awk '{print $1}'`
if [ "$size" -eq "0" ]; then
echo "File: \"$base1\" was found same in both directories."
cp "$i" "$path3"
echo "Copied to \"$path3\" successfully!"
fi
fi
fi
done
fi
done
rm $temp
: << --
OUTPUT (Attempt 1)
rushyang@Maverick_Meerkat: GTU-MCA $ bash GTU6-2.sh
Enter first Directory's Absolute path: /home/rushyang/sldjf
Invalid path, Try Again!
Enter first Directory's Absolute path: /home/rushyang/Experiments/1510/mydir1/
Enter second Directory's Absolute path: /home/rushyang/lsajlj
Invalid path, Try Again!
Enter second Directory's Absolute path: /home/rushyang/Experiments/1510/mydir2/
Enter Third Directory's Path, to copy files in case of exact match: /home/rushyang/Experiments/1510/mydir3/
File: "1" was found same in both directories.
Copied to "/home/rushyang/Experiments/1510/mydir3/" successfully!
File: "new file" was found same in both directories.
Copied to "/home/rushyang/Experiments/1510/mydir3/" successfully!
OUTPUT (Attempt 2)
rushyang@Maverick_Meerkat: GTU-MCA $ bash GTU6-2.sh
Enter first Directory's Absolute path: /home/rushyang/Experiments/1510/mydir1/
Enter second Directory's Absolute path: /home/rushyang/Experiments/1510/mydir2/
Enter Third Directory's Path, to copy files in case of exact match: /home/rushyang/Experiments/1510/mydir3/
File: "1" was found same in both directories.
Copied to "/home/rushyang/Experiments/1510/mydir3/" successfully!
File: "new file" was found same in both directories.
Copied to "/home/rushyang/Experiments/1510/mydir3/" successfully!
--
######################## OR ########################
#!/usr/bin/env bash
# Code written By: Rushyang Darji
# Last Build: 02.10.2010
# Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts.
: << --
GTU 6: Write a script to compare identically named files in two different directories and if they are same, copy
one of them in a third directory
Code Developed By: Rushyang Y Darji (rushyang@yahoo.co.in)
Init Build: 04.08.2010
Last Build: 15.10.2010
v1.6
--
clear
temp=$(mktemp tmp.XXXXXXXX) #mktemp makes temporary file into /temp directory with r+w permissions only for creator.
echo -n "Enter 1st Directory: "
read dir1
PATH1="$(pwd)/$dir1" #Converted whole path of dir1 into PATH1 variable
echo "PATH1=$PATH1"
main=$(pwd)
if test -d $PATH1; then #Test condition to make sure dir1 is a directory.
echo -n "Enter 2nd Directory: "
read dir2
PATH2="$(pwd)/$dir2" #Same as Line 14
echo "PATH2=$PATH2"
if test -d $PATH2; then
cd $PATH1 #Secured for using for i loop.
for i in *
do
cd $PATH2 #Secured for using for j loop.
for j in *
do
if test "$i" == "$j"; then
cd $main #Back to $(basedir)
cmp "$PATH1/$i" "$PATH2/$j" > $temp
# cmp checks byte by byte.. can be little slower than 'diff'
size=`ls -s $temp | awk '{print $1}'` # '-s' for listing size, and 'awk' for fetching size of $temp
if test "$size" == "0"; then # if size of temporary file is 'ZERO', then both files are exactly same.
echo "File: \"$i\" was found same in both directories."
echo -n "Enter Directory name (must be in current working directory) to copy it: "
read dir3
PATH3="$(pwd)/$dir3" #Same as Line 14
if test -d $PATH3; then
cp -i "$PATH1/$i" "$PATH3" # "$PATH2/$j" instead of "$PATH1/$i" will also do.
else
echo "There is no directory named $dir3 in $pwd" #Line 41
fi
fi
fi
done
done
else
echo "Invalid Directory Name for dir2." #Error Message of missing dir2
fi
else
echo "Invalid Directory Name for dir1." #Error Message of missing dir3
fi
rm $temp
: << --
rushyang@Maverick_Meerkat: 1510 $ ls mydir1/
1 2 new file
rushyang@Maverick_Meerkat: 1510 $ ls mydir2/
1 new file T
rushyang@Maverick_Meerkat: 1510 $ ls mydir3/
OUTPUT (Attempt 1)
Enter 1st Directory: asdf
PATH1=/home/rushyang/Experiments/1510/asdf
Invalid Directory Name for dir1.
OUTPUT (Attempt 2)
Enter 1st Directory: mydir1
PATH1=/home/rushyang/Experiments/1510/mydir1
Enter 2nd Directory: mydir2
PATH2=/home/rushyang/Experiments/1510/mydir2
File: "1" was found same in both directories.
Enter Directory name (must be in current working directory) to copy it: mydir3
File: "new file" was found same in both directories.
Enter Directory name (must be in current working directory) to copy it: mydir3
OUTPUT (Attempt 3)
Enter 1st Directory: mydir1
PATH1=/home/rushyang/Experiments/1510/mydir1
Enter 2nd Directory: mydir2
PATH2=/home/rushyang/Experiments/1510/mydir2
File: "1" was found same in both directories.
Enter Directory name (must be in current working directory) to copy it: mydir3
cp: overwrite `/home/rushyang/Experiments/1510/mydir3/1'? y
File: "new file" was found same in both directories.
Enter Directory name (must be in current working directory) to copy it: mydir3
--
Shared By : Divya S Didwania (LCIT) Show/Hide Program
6.Write a script to compare identically named files in two different directories and if they are same, copy
one of them in a third directory.
******************************************************************
echo Enter the name of 1st directory
read dir1
echo Enter the name of 2nd directory
read dir2
echo Enter the name of the directory in which you want to copy the duplicate file
read dir3
cd `find /home/divya -type d -name $dir1`
for fl1 in *
do
echo $fl1
cd `find /home/divya -type d -name $dir2`
for fl2 in *
do
if [ $fl1 == $fl2 ]
then
cd ..
cp ./$dir2/$fl2 ./$dir3
cd `find /home/divya -type d -name $dir2`
rm $fl2
cd `find /home/divya -type d -name $dir1`
rm $fl1
fi
done
cd ..
mv ./temp1/* ./$dir2/$fl2
done
~
~
~
~
~
"6.sh" 29L, 876C written
[divya@linux ~]$ sh 6.sh
Enter the name of 1st directory
d1
Enter the name of 2nd directory
d2
Enter the name of the directory in which you want to copy the duplicate file
d3
first
[divya@linux ~]$
******************************************************************
Shared By : diz Show/Hide Program
echo "Enter 1st file full path(/home/mca/diz/) :"
read p1
echo "Enter 2nd file full path(/home/mca/diz/) :"
read p2
echo "Enter new path for copy"
read dest
a=`cmp $p1 $p2`
if [ $? -eq 0 ]
then
`cp $p2 $dest`
echo "File successfully copied on path: "$dest
else
echo "File not identically same"
fi
7. Write a script to delete zero sized files from a given directory ...
#!/usr/bin/env bash
# Code Developed By: Rushyang Darji
# Last Build: 14.09.2010
# Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts.
while true; do
read -e -p "Enter the Absolute Path: " path || exit
[[ -d $path ]] && break
echo "Invalid Directory!"
done
args=`find "$path" -empty -print0 | xargs -0`
for i in $args
do
if [ -f "$i" ]; then
rm -i "$i"
fi
done
Shared By : Divya S Didwania (LCIT) Show/Hide Program
7.Write a script to delete zero sized files from a given directory (and all its sub-directories).
******************************************************************
echo Enter the file you want to delete
read zerofile
if [ ! -f $zerofile ]
then
echo "File is does not exists"
elif [ ! -s $zerofile ]
then
rm -i $zerofile
else
echo " file has some data"
fi
"7.sh" 19L, 303C written
[divya@linux ~]$ sh 7.sh
Enter the file you want to delete
el.lst
File is does not exists
[divya@linux ~]$ ls
6.sh 71.sh 7.sh d1 d2 d3 e.lst emp.lst temp1
[divya@linux ~]$ sh 7.sh
Enter the file you want to delete
e.lst
rm: remove regular empty file `e.lst'? y
[divya@linux ~]$ ls
6.sh 71.sh 7.sh d1 d2 d3 emp.lst temp1
or
echo Enter the file extention you want to delete :
read ext
if [ -n *.$ext ]
then
rm -i *.$ext
else
echo No such extension file
fi
for i in file *
do
if [ ! -s $zerofile ]
then
rm -i $zerofile
fi
done
******************************************************************
Shared By : diz Show/Hide Program
for i in `find /home/mca/diz/diz -type f -size 0c`
do
echo "$i has Zero size"
echo `rm $i`
done
8. Write a script to display the name of those files (in the given directory...
#!/usr/bin/env bash
# Write a script to display the name of those files (in the given directory) which are having multiple links.
# Developed By: Rushyang Darji
# Init Build: 15.10.2010
# Last Build: 15.10.2010
# v1.0
# Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts.
echo "Enter Absolute path of directory"
read path
if test -d $path; then
cd $path
for i in *
do
for j in *
do
if test "$i" != "$j"; then
if test "$i" -ef "$j"; then
echo "$i" >> $$.temp
fi
fi
done
done
cat $$.temp | uniq
rm $$.temp
cd -
else
echo "Check your path."
fi
############### OR ###############
#!/usr/bin/env bash
# Write a script to display the name of those files (in the given directory) which are having multiple links.
# Developed By: Rushyang Darji
# Init Build: 15.10.2010
# Last Build: 15.10.2010
# v1.0
# Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts.
temp=$(mktemp tmp.XXXXXXXXXX)
while true; do
read -e -p "Ente the Absolute Path: " path || exit
[[ -d $path ]] && break
echo "Invalid Directory!"
done
for i in $path/*
do
for j in $path/*
do
base1=`basename $i`
base2=`basename $j`
echo "$i"
echo "$j"
if test "$base1" != "$base2"; then
if test "$base1" -ef "$base2"; then
echo "$base1" >> $temp
fi
fi
done
done
cat $temp | uniq
rm $temp
Shared By : Your Name (College - Place ) Show/Hide Program
Share this program...
Send it to gtumca@gmail.com
with your Name - College name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
9. Write a script to display the name of all executable...
#!/usr/bin/env bash
# GTU 9
# Code Developed by: Rushyang Darji
# Last Updated: 14.10.2010
# Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts.
# Remember, You should NEVER parse the output of "ls" command and use into scripts. Because UNIX filesystem allows all kind of characters to assign a filename, including "new line". For more info.. Google "parsing output of ls".
# read -e enables Tab Completion which is way too easier than entering whole absolute path.
read -e -p "Enter Absolute Path: " path || exit
temp=$(mktemp)
if test -d $path; then
for i in $path/*
do
if test -x "$i"; then
echo "$i" >> "$temp"
fi
done
else
echo "Invalid Directory"
fi
cat $temp
rm $temp
Shared By : Divya S Didwania (LCIT) Show/Hide Program
9.
Write a script to display the name of all executable files in the given directory.
******************************************************************
i=0
ls > test1
set `wc -l test1`
l=$1
while [ $l -ne 0 ]
do
line=`head -$l test1 | tail -1`
if [ -x $line ]
then
i=`expr $i + 1`
fi
l=`expr $l - 1`
done
echo -e "\t\t\tTotal no. of executable files : "
echo $i
~
~
~
~
"9.sh" 15L, 267C written
[divya@linux ~]$ ls -l
total 136
-rw-rw-r-- 1 divya divya 395 2010-07-31 16:01 13.sh
-rw-rw-r-- 1 divya divya 108 2010-07-31 14:58 2.sh
-rw-rw-r-- 1 divya divya 131 2010-07-31 15:31 3.sh
-rw-rw-r-- 1 divya divya 204 2010-07-31 15:28 5.sh
-rw-rw-r-- 1 divya divya 848 2010-07-29 16:40 6.sh
-rw-rw-r-- 1 divya divya 267 2010-07-31 16:07 9.sh
drwxrwxr-x 2 divya divya 4096 2010-07-30 13:38 d1 -------------
drwxrwxr-x 2 divya divya 4096 2010-07-29 16:41 d3 ------------
-rw-rw-r-- 1 divya divya 122 2010-07-31 15:17 exist.sh
-rw-rw-r-- 1 divya divya 182 2010-07-31 14:59 f
-rw-rw-r-- 1 divya divya 182 2010-07-31 14:58 first
-rw-rw-r-- 1 divya divya 257 2010-07-29 16:37 login.sh
-rw-rw-r-- 1 divya divya 182 2010-07-31 14:55 s
-rw-rw-r-- 1 divya divya 260 2010-07-31 15:53 s1
drwxrwxr-x 2 divya divya 4096 2010-07-29 16:41 temp1 ------------------
-rw-rw-r-- 1 divya divya 25 2010-07-31 15:13 temp2
-rw-rw-r-- 1 divya divya 86 2010-07-31 16:06 test1
[divya@linux ~]$ sh 9.sh
Total no. of executable files : 3
******************************************************************
Shared By : Your Name (College - Place ) Show/Hide Program
Share this program...
Send it to gtumca@gmail.com
with your Name - College name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
10.Write a script to display the date, time and a welcome ...
#!/usr/bin/env bash
# GTU10: Write a script to display the date, time and a welcome message (like Good Morning should be displayed with “a.m.” or “p.m.” and not in 24 hours notation.
# Code written By: Rushyang Darji
# Last Updated: 19.08.2010
# Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts.
msg2=`date +%H`
echo "Welcome $USERNAME!"
time=`date +"%F %I:%M:%S %p"`
echo "Current Time is: $time"
if [[ "$msg2" -ge "5" ]] && [[ "$msg2" -lt "12" ]]; then
echo "Good Morning..!"
elif [[ "$msg2" -ge "12" ]] && [[ "$msg2" -lt "17" ]]; then
echo "Good Afternoon..!"
elif [[ "$msg2" -ge "21" ]] && [[ "$msg2" -lt "19" ]]; then
echo "Good Evening..!"
else
echo "Good Night..!"
fi
Shared By : Divya S Didwania (LCIT) Show/Hide Program
10
Write a script to display the date, time and a welcome message (like Good Morning etc.). The time
should be displayed with “a.m.” or “p.m.” and not in 24 hours notation
******************************************************************
tput clear
hr=`date +%I`
set `who am i`
a=`echo $1`
set `date +%r`
b=`echo $1 $2`
if [ $hr -lt 12 ]
then
echo "welcome `$a $b` Good Morning"
elif [ $hr -ge 12 ]
then
echo "Welcome $a $b GOOD Afternoon"
elif [ $hr -ge 16 -o $hr -le 20 ]
then
echo "Good Evening"
else
echo "God Night"
fi
or
tput clear
hr=`date +%H`
if [ $hr -lt 12 ]
then
echo "welcome `who` Good Morning"
elif [ $hr -gt 12 ]
then
echo "Welcome `who` GOOD Afternoon"
elif [ $hr -eq 17 || $hr -lt 24 ]
then
echo "Good Evening"
else
echo "God Night"
fi
~
~
Output
[Divya@linux ~]$ sh time.sh
Welcome GOOD Afternoon
or
time=`date +%H%M%S`
d1=`date +%d/%m/%y`
t1=`date +%I:%M:%S`
echo "date:"$d1
if [ $time -ge 000000 -a $time -lt 120000 ]
then
echo "time:"$t1" a.m"
else
echo "time:"$t1" p.m"
fi
if [ $time -lt 120000 -a $time -gt 60000 ]
then
echo "GooD MorninG"
else
if [ $time -eq 120000 ]
then
echo "GooD NooN"
else
if [ $time -gt 120000 -a $time -lt 180000 ]
then
echo "GooD AfterNooN"
else
if [ $time -gt 180000 -a $time -lt 210000 ]
then
echo "GooD EveninG"
else
echo "GooD NighT"
fi
fi
fi
fi
"101.sh" 33L, 661C written
[divya@linux ~]$ sh 101.sh
date:04/10/10
time:12:35:28 p.m
GooD AfterNooN
[divya@linux ~]$
or
clear
date >dt
s=`cut -c 12-13 dt`
echo $s
if [ $s -lt 12 ]
then
echo "GOOD MORNING"
elif [ $s -gt 12 -o $s -lt 16 ]
then
echo "GOOD AFTERNOON"
elif [ $s -gt 16 -o $s -lt 20 ]
then
echo "GOOD EVENING"
else
echo "good night"
fi
~
~
~
16
GOOD AFTERNOON
[divya@linux ~]$
****************************************************************
Shared By : diz Show/Hide Program
time=`date +%H%M%S`
d1=`date +%d/%m/%y`
t1=`date +%I:%M:%S`
echo "date:"$d1
if [ $time -ge 000000 -a $time -lt 120000 ]
then
echo "time:"$t1" a.m"
else
echo "time:"$t1" p.m"
fi
if [ $time -lt 120000 -a $time -gt 60000 ]
then
echo "GooD MorninG"
else
if [ $time -eq 120000 ]
then
echo "GooD NooN"
else
if [ $time -gt 120000 -a $time -lt 180000 ]
then
echo "GooD AfterNooN"
else
if [ $time -gt 180000 -a $time -lt 210000 ]
then
echo "GooD EveninG"
else
echo "GooD NighT"
fi
fi
fi
fi