Showing posts with label backtrack learning exercise. Show all posts
Showing posts with label backtrack learning exercise. Show all posts

Wednesday, 12 June 2013

How to display many files in Backtrack

How to display many files in Backtrack

Write a script, which displays one or more files on the monitor, each with its own title page, notify user which files were displayed on the monitor and which were not.

Code

#!/bin/bash
echo " Current directry displayed below from the following list "
ls
echo "Press Enter to run the loop"
read b
files=`ls`
i=19
for f in $files
do
echo "----------------------------------------------------------"
echo "$i Title $f   ..."
cat $f
let i-=1
echo "

Loop Count is $i"
echo "-----------------------------------------------------------"
sleep 2
done

Screen Shot


How to display many files in Backtrack

How to display Good Morning in Backtrack

How to display Good Morning in Backtrack

Write a script, which displays Good Morning if hour is less than 12, Good Afternoon if hour is less than5 p.m. and Good Evening if hour is greater than 5 p.m.

Code

#!/bin/bash
clear
hour=$(date +%H)
echo”-----------------------------------------”
echo”the yet time now”
if [ "$hour" -lt 12 ]; then
echo "Good Morning"
elif [ "$hour" -lt 17 ]; then
echo "Good Afternoon"
elif [ "$hour" -gt 17 ]; then
echo "Good Morning"
fi

Screen Shot

How to display Good Morning in Backtrack

How to check files in Backtrack

How to check files in Backtrack

Write a script, which copies file1 to file2, check to see whether file2 exists already, if file2 exists then check that whether there is write permission on file2 or not, if there is write permission on file2 then warn user before copying file1 to file2, and get permission to proceed else exits, otherwise copy files.

Code

#!/bin/bash
clear
echo " ==== File Copying Program ==== "
echo " "
echo "These are the files in present directory"
ls
echo ""
echo "Please enter source filename "
read a
vre=`pwd`
if [ -f $vre/$a ];
then
echo " "
else
echo "Enter valid file name"
exit 0
fi
echo "Please enter destination filename "
read b
vre1=`pwd`
if [ -f $vre1/$b ];
then
echo "File dont have write permissions"
echo "Do you want to change mode and allow writting ? , (y)es or (n)o "
read d
case $d in
y)
chmod 600 $b
cp $a $b
echo " File Copied Successfully"
exit 0
;;
n)
exit 0
;;
esac
else
echo "File Name Invalid in present directory"
exit 0
fi
else
echo "Enter valid file name"
exit 0 

Screen Shot

How to check files in Backtrack

Show recent files in Backtrack

Show recent files in Backtrack

Write a script, which lists the most recent file in a directory, if user type recent <Return> then names of the ten most recently modified files are displayed . If the user types recent n <Return> then names of the n most recently modified files are displayed otherwise, the user is notified of incorrect usage.

Code

#!/bin/bash
cd /root/noman2/temp
let "a =1";
while test $a -eq 1;
do
clear
echo " === Recent Files Program == "
echo " "
echo "Chose from the following options: "
echo " Enter 'm' to view the last modified file"
echo " Enter 'recent' to view last 10 recent files"
echo " Enter 'recent n' to view last n recent files"
echo " Enter 'q' to quit "
echo ""
echo "Enter your Option"
read c
clear
case $c in
m)
ls -tr | tail -1
read x
;;
recent)
ls -t | head
read x
;;
q)
exit 0
;;
*)
v1=${c#* }
if (ls -t | head -n $v1); then
read x
else
echo "Please enter valid option"
read x
fi
;;
esac
done

Screen Shot


Show recent files in Backtrack

Delete files from Backtrack

Delete files from Backtrack

Write a script that allows user to display a file on the monitor, delete a file, or quit the program.

Code
clear
let "a = 1";
while test $a -eq 1;
do
clear
echo " ====== File Viewing Program ====== "
echo " "
echo " Choose from the following: "
echo " 1 ; List all files "
echo " 2 ; Display a file on screen "
echo " 3 ; Delete a file "
echo " q ; Quit "
echo " "

echo -n "Enter Option: "
read c
clear
case "$c" in
1)
echo "Files in the present directory:"
ls
echo " "
read x
clear
;;
2)
echo "Enter the file name which you want to view "
read c
if (cat $c); then
read x
else
echo "File Not Found"
read x
fi
clear
;;
3)
echo "Enter the file name which you want to delete"
read c
if (rm $c); then
echo "File deleted successfully"
read x
else
echo "File Not Found"
read x
fi
;;
q)
exit 0
;;
esac
done

Screen Shot

Delete files from Backtrack

Script show date in Backtrack

Script show date in Backtrack

Write a script that displays about “Who is Logged In” and “Date”.

code

#!/bin/bash
Clear
echo " Script no.1 "
echo " ------------------------ "
echo " Command Menu "
echo " ------------------------ "
echo "PRESS 1 FOR Users"
echo "PRESS 2 FOR DATE"
echo "PRESS q FOR QUIT"
while test 1
do
read a
case $a in
1)
echo "Login Name: $(echo $USER)"
who
;;
2)
echo "System date : $(date)"
;;
q)
exit 0
;;
esac
done 

Screen Shot

Script show date in Backtrack

Thursday, 6 June 2013

Simple script that copy files to directory in backtrack


Simple script that copy files to directory in backtrack 

 Write a script that copy files to directory /temp, remove them from the current directory, clean up anything a user has in the /temp directory over ten (10) days old. Finally send mail to user.

Code 

#!/bin/bash
While true
do
echo " Script no.1 "
echo " ------------------------ "
echo " Command Menu "
echo " ------------------------ "
echo "1.Copy the file to the temp directory"
echo "2. Remove files that are over 10 days old from the temp directory"
echo "3. Send mail to user"
echo "q. Quit"
echo " ------------------------ "
echo -n "Input Your Choice : "
read b
case "$b" in
1)
echo -n "Enter the filename to be copied : "
read b
if( mv $b \temp) ; then
echo "File has been copied to the temp directory, Successfully !"
read b
else
echo "Error!"
read b
fi
;;
2)
find /root/noman/temp/* -mtime -10 -exec rm {} \;
echo "files that were over 10 days old are deleted successfully"
ls -1 temp
read b
;;

3)
mail
;;
q)
exit 0
;;
*)
echo " Invalid Choice... Try Again "
sleep 2
;;
esac

done

Screen Shot

backtrack learning exercise