21. Write a script to check whether a given number is...
Shared By : Divya S Didwania (LCIT) Show/Hide Program
21 Write a script to check whether a given number is palindrome or not.
******************************************************************
clear
echo "ENTER NUMBER :="
read number
rem=0
i=10
revnumber=0
for((;number>0;))
do
let revnumber=" revnumber * 10 "
let rem=" number % 10 "
let number=" number / 10 "
let revnumber=" revnumber + rem "
done
echo -e "\nREVERSE NUMBER :-\c" & echo $revnumber
~
~
~
~
~
ENTER NUMBER :=\c
435621
REVERSE NUMBER :-126534
[divya@linux ~]$
******************************************************************
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
22. Write a script to display all words of a file in ascending order.
Shared By : Divya S Didwania (LCIT) Show/Hide Program
22.Write a script to display all words of a file in ascending order.
******************************************************************
echo " Enter File Name"
read fname
sort $fname
or
echo -e "\n enter File name "
read fn
for i in `cat $fn`
do
echo $i >> f2.txt
done
sort f2.txt
rm f2.txt
~
~
[divya@linux ~]$ cat > f1
z
x
y
w
n
h
t
r
u
a
b
c
[11]+ Stopped cat > f1
[divya@linux ~]$ sh 22.sh
enter File name
f1
a
b
c
h
n
r
t
u
w
x
y
z
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
23. Write a script to display all lines of a file in ascending order.
****************************************************************************************
23 Write a script to display all lines of a file in ascending order
****************************************************************************************
echo " Enter File Name"
read fname
sort $fname
------------------------------------Output -----------------------------------------------
[divya@linux ~] sh 23.sh
Enter File Name
second.txt
1
10
2
4
40
5
Shared By : Hetal Modi (CTI- Gandhinagar) Show/Hide Program
23. Write a script to display all lines of a file in ascending order.
echo " Enter File Name"
read fname
sort $fname
/* output
[mca0914@ctilinux-1 mca0914]$ sh 23.sh
Enter File Name
second.txt
1
10
2
4
40
5
8
*/
24. Write a script to display the last modified file.
Shared By : Rushyang Darji (SVIT -Vasad ) Show/Hide Program
#!/usr/bin/env bash
# GTU24: Write a script to display the last modified file.
# Code written By: Rushyang Darji
# Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts.
# Final Build: 19.10.2010
while true;
do
# -e enables readline, which means you can use tab-completion. & -p prints whatever's written in "" before taking the path
# The ''|| exit'' makes the script exit if read returns false, which it
# will if the user hits Ctrl+C amongst other.
read -e -p "Enter Directory: " path || exit
# if path contains an existing directory, break out of this infinite loop.
[[ -d $path ]] && break
echo "Invalid Path, Try Again!"
done
cd $path # cd $path is inevitable because of * in use of ls.
ls * -dpltr | grep -v '/$' | tail -n1
# Here, observe '*' after ls. You must specify a wildcard pattern for indicating all files first.
# This is because the -d option specifies that only directory names should listed.
# Moreover, -p puts an indicator at the end of "directories", which will be stripped by grep inverse.
# Once we have neglected directories, we can list(-l) "ONLY FILES" from current working directory sorted by it's modified time (-t) in reverse order (-r). The most last one will be fetched by tail.
cd $OLDPWD
# OLDPWD is the env var, which always remembers our "PREVIOUS WORKING DIRECTORY". Enter `env | grep OLDPWD` to see it.
# 'cd -' will also lead us into last working directory. But then also we don't need to print it on Terminal while executing it.
25. Write a shell script to add the statement #include ...
**************************************************************************************************************************
25 Write a shell script to add the statement #include <stdio.h> at the beginning of every C source file in
current directory containing printf and fprintf.
**************************************************************************************************************************
while true; do
read -e -p "Enter Directory: " path || exit
[[ -d $path ]] && break
echo "Invalid path! Try Again!"
done
path=${path%/}
myargs=`grep -l -e "printf" -e "fprintf" $path/*.c | xargs`
if [ $? -gt 1 ]; then
echo -n "No Matches were found. " && exit
fi
temp=$(mktemp tmp.XXXXXXXXX)
for i in $myargs # Here, grep has the exit status 0.
do
echo "Do you want to add '#include <stdio.h>' to $i?"
read S
case $S in
Y|y|YES|Yes|yes|yeah)
sed '1i\
#include<stdio.h>' "$i" > "$temp"
mv "$temp" "$i" ;;
n|N|NO|no|No|nope)
echo "Alright! Next.."
shift ;;
*)
echo "Invalid input." ;;
esac
done
if [ -z $myargs ]; then
echo "No Matches were found. Try another Directory"
else
clear
head -n5 $path/*.c | less
fi
rm $temp
-------------------------Output--------------------------------------------------
[divya@linux ~]$ sh test2.sh
Enter Directory: /home/divya
/home/divya
/home/divya/p1.c /home/divya/p.c
tmp.nXpY26411
Do you want to add '#include <stdio.h>' to /home/divya/p1.c?
n
Alright! Next..
Do you want to add '#include <stdio.h>' to /home/divya/p.c?
y
==> /home/divya/p1.c <==
#include<stdio.h>
printf"hiii"
==> /home/divya/p.c <==
#include<stdio.h>
#include<stdio.h>
#include<stdio.h>
printf "hi"
fprintf"hello:"
Shared By : Rushyang Darji (SVIT -Vasad ) Show/Hide Program
#!/usr/bin/env bash
# Code written By: Rushyang Darji
# Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts.
# Final Build: 02.10.2010
while true; do
read -e -p "Enter Directory: " path || exit
[[ -d $path ]] && break
echo "Invalid path! Try Again!"
done
path=${path%/}
myargs=`grep -l -e "printf" -e "fprintf" $path/*.c | xargs`
if [ $? -gt 1 ]; then # grep exits with status 1 when no matches were found.
echo -n "No Matches were found. " && exit
fi
temp=$(mktemp tmp.XXXXXXXXX)
for i in $myargs # Here, grep has the exit status 0.
do
echo "Do you want to add '#include <stdio.h>' to $i?"
read S
case $S in
Y|y|YES|Yes|yes|yeah)
sed '1i\
#include<stdio.h>' "$i" > "$temp" # i for insertion, 1 for 1st line. $i is the file to insert. and all output will be redirected to $temp
mv "$temp" "$i" # renaming $temp by over writing to $i
;;
n|N|NO|no|No|nope)
echo "Alright! Next.."
shift
;;
*)
echo "Invalid input."
;;
esac
done
if [ -z $myargs ]; then
echo "No Matches were found. Try another Directory"
else
clear
head -n5 $path/*.c | less
fi
rm $temp
######################## OR ########################
#!/usr/bin/env bash
# GTU25: Write a shell script to add the statement #include <stdio.h> at the beginning of every C source file in current directory containing printf and fprintf.
# Code written By: Rushyang Darji
# Visit My Online Repository: "http://github.com/rushyang/GTU-OS-Bash-Scripts" for regular updates and more scripts.
# Final Build: 19.08.2010
# If you want to rename and include all ".C" files too, Refer GTU26.sh
myargs=`grep -l -e "printf" -e "fprintf" *.c | xargs`
if [ $? -gt 1 ]; then # grep has exit status 1 when no matches were found.
echo -n "No Appropriate Matches were found. "
fi
for i in $myargs # Here, grep has the exit status 0.
do
echo "Do you want to add '#include <stdio.h>' to $i?"
read S
case $S in
Y|y|YES|Yes|yes|yeah)
sed '1i\
#include<stdio.h>' "$i" > $$ # i for insertion, 1 for 1st line. $i is the file to insert. and all output will be redirected to $$ (or you can say temp... Actually $$ returns terminal id. but when output is directed to it, a file is made)
mv $$ $i # renaming $$ by over writing to $i
;;
n|N|NO|no|No|nope)
echo "Alright!"
shift
;;
*)
;;
esac
done
if [ -z $myargs ]; then
echo "No Matches were found. Try another Directory"
else
clear
head -n5 *.c | less
fi
26. Write a script that behaves both in interactive and...
**************************************************************************************************************************
26 Write a script that behaves both in interactive and non-interactive mode. When no arguments are
supplied, it picks up each C program from current directory and lists the first 10 lines. It then prompts
for deletion of the file. If the user supplies arguments with the script, then it works on those files only.
**************************************************************************************************************************
N=$#
ext=c
if test "$N" -eq "0"; then
while true; do
read -e -p "Enter Path: " path || exit
[[ -d $path ]] && break
echo "Invalid Path, Try Again!"
done
path=${path%/}
for i in $path/*.C
do
if [ "$i" != $path/'*.C' ]; then
mv "$i" "${i/.C/}".c
clear
fi
done
for i in $path/*."$ext"
do
if [ "$i" != "$path"/'*.c' ]; then
clear
echo "File is $i"
head -n10 "$i" | nl
sleep 1
rm -i "$i"
else
echo "There are no matching \"C\" files to Prompt in this directory."
sleep 2
clear=no
fi
done
if test "$clear" != "no"; then
clear
echo "Remaining C files in the Directory..."
ls -1 $path/*.c
fi
else
for i in $path/*.C
do
if [ "$i" != $path/'*.C' ]; then
mv "$i" "${i/.C/}".c
clear
fi
done
for i in $*
do
clear
i="${i/.c/}"
i="$(pwd)/$i.c"
if [ -f "$i" ]; then
echo "File name is $i"
head -n10 "$i" | nl
sleep 1
rm -i "$i"
else
echo "There is no such a file with name: \"$i\" in current working directoy"
sleep 3
fi
done
clear
echo "Remaining C files in the Directory..."
ls -1 *.c
sleep 1
fi
-------------------------------------------Output----------------------------------------------------------------------
[divya@linux ~]$ sh 26.sh
#if argument is not pass
Enter Path: /home/divya
File is /home/divya/p1.c
1 #include<stdio.h>
2 #include<stdio.h>
3 printf"hiii"
rm: remove regular file `/home/divya/p1.c'? n
File is /home/divya/p.c
1 h
2 e
3 l
4 l
5 o
6 w
7 o
8 r
9 l
10 d
rm: remove regular file `/home/divya/p.c'? y
#if argument is pass
[divya@linux ~]$ sh 26.sh p2.c
File name is /home/divya/p2.c
1 h
2 r
3 u
4 f
5 ine
6 or
7 not
8 by divya
9 k
10 bye
rm: remove regular file `/home/divya/p2.c'? n
Remaining C files in the Directory...
p1.c
p2.c
Remaining C files in the Directory...
/home/divya/p1.c
**************************************************************************************************************************
Shared By : Rushyang Darji (SVIT -Vasad ) Show/Hide Program
# !/usr/bin/env bash Rushyang Darji (SVIT -Vasad )
# 26. Interactive - non-interactive shell script to prompt and delete c files within the given or predefined current directory.
#
# Code Developed By: Rushyang Darji
# Init. Build: 06.08.2010
# Last Build: 19.10.2010
N=$#
ext=c
if test "$N" -eq "0"; then
while true; do # Same inifinite loop as we used in GTU24
read -e -p "Enter Path: " path || exit
[[ -d $path ]] && break
echo "Invalid Path, Try Again!"
done
path=${path%/} # Removes last / from the end of the path. Though, it's not compulsion to do so because /foo/bar and /foo//////bar is considered exactly the same!
for i in $path/*.C
do
if [ "$i" != $path/'*.C' ]; then # If there is no match, Value of i will be ''$path/*.C''. & That's why there is no need to rename.
mv "$i" "${i/.C/}".c # Renames every .C files to .c, so that we can use it afterwards in same loop.
clear
fi
done
for i in $path/*."$ext"
do
if [ "$i" != "$path"/'*.c' ]; then # If there is no ".C FILE" exist in that directory, it will switch to else.
clear
echo "File is $i"
head -n10 "$i" | nl # head for displaying First 10 lines, nl for numbering them on terminal.
sleep 1 # Halt for 1 second
rm -i "$i" # -i for interactive prompt.
# Remember, "" around $i is super necessary! Because except it, you'll get an error with filenames containing spaces.
else
echo "There are no matching \"C\" files to Prompt in this directory."
sleep 2
clear=no
fi
done
if test "$clear" != "no"; then # If clear=no then there are no C Files to display.
clear
echo "Remaining C files in the Directory..."
ls -1 $path/*.c # 1 result per line (-1)
fi
else # Else part contains, where user passes the name of C files, which should exist in the current working directory as the parameter.
for i in $path/*.C
do
if [ "$i" != $path/'*.C' ]; then # if There are no matches ie if there is no C file in given dir, 'i' will be ''$path/*.C''
mv "$i" "${i/.C/}".c # Renames every .C files to .c
clear
fi
done
for i in $* # When filenames are passed as parameters.
do
clear
i="${i/.c/}" # Removes an extension from file variable 'i' Only in the case of extension is also passed within the filename parameter.
i="$(pwd)/$i.c" # Makes i the complete path of a file, including extension..
# Last two lines are necessary because user, may and may not enter filename including extension.
if [ -f "$i" ]; then # Checks for the existence of given filename, into pwd
echo "File name is $i"
head -n10 "$i" | nl
sleep 1
rm -i "$i"
else # Error for non-Existent files.
echo "There is no such a file with name: \"$i\" in current working directoy"
sleep 3
fi
done
clear
echo "Remaining C files in the Directory..."
ls -1 *.c
sleep 1
fi
27. Write a script that deletes all leading and trailing spaces...
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