simple Shell program for pro's ..but need some help
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#! /bin/bash
# The temporary files used by the script
tmp=/tmp/nmapped.$$
ping_temp=/tmp/ping.$$
sharefile=/tmp/sharefile.$$
signal_func() {
echo
tput setf 4
echo -n "Are you sure you want to quit :: "
tput sgr0
read QUIT_NOW
QUIT_NOW=`echo "${QUIT_NOW}" | tr 'a-z' 'A-Z'`
trap - INT
if [ "${QUIT_NOW}" == "Y" ] || [ "${QUIT_NOW}" == "YES" ] ; then
exit 0
else
continue
fi
}
trap 'signal_func' INT
DOIT=Y
while [ $DOIT = "Y" -o $DOIT = "y" ]
do {
date
# Asks you what you want to ping and then puts it out as the variable
if [ $USER != "root" ] ; then
echo ""
tput bold
echo "WARNING! You must be logged in as 'root' for this script to run properly!"
tput sgr0
else
echo ""
fi
echo ""
echo "----------------------"
tput setf 2
echo "(NOTE: THE PID FOR THIS APPLICATION IS $$)"
tput sgr0
echo ""
tput bold
echo -n "What server do you want to test? (Or type \"quit\" to exit) :: "
tput sgr0
read server
while [ "$server" = "quit" ] || [ "$server" = "" ]
do
if [ "$server" == quit ] ; then
clear
exit 0
fi
if [ "$server" == "" ] ; then
echo "Gees, are you not going to enter anything?"
echo
echo "Exiting..."
exit 1
fi
done
echo ""
echo "----------------------"
echo "Pinging $server....."
echo
# Pings the server that was entered as the server variable
# ping -c 4 $server 2>&1 | grep avg | awk '{print $4}' | cut -d'/' -f2 > $ping_temp
grep avg <(ping -c 4 $server 2>&1) | awk '{print $4}' | cut -d'/' -f2 > $ping_temp
# Checks the server status and then advises of it's status
if [ -s $ping_temp ] ; then
echo "$server responds to ping"
else
echo "$server doesn't appear to be up" ; exit 1
fi
echo
echo "Average reply time - `cat $ping_temp`ms"
echo
echo "-----------------------"
echo
# Checks server and gets port info
tput bold
echo -n "Do you want detailed information for this server? (y/n) :: "
tput sgr0
read detail
if [ $detail == "n" -o $detail == "N" ] ; then
nmap $server 2>&1 | grep open > $tmp
else
nmap -A -T4 -F $server 2>&1 | grep open > $tmp
fi
count=`wc -l $tmp | awk '{print $1}'`
echo "There are $count ports open on \"$server\""
echo
echo "-----------------------"
echo
# Prints lists of open ports on server
cat $tmp
rm $tmp
rm $ping_temp
echo
echo "-----------------------"
echo
}
DOIT=N
tput bold
echo -n "Do you want to ping another host? (y/n) :: "
tput sgr0
read DOIT
DOIT=`echo $DOIT | tr '[a-z]' '[A-Z]'`
done
and there is a thing tp disable the functionality of the special characters like the * or /
you can put : set -f beofre you want to handle them , and then when you want them back as special chars you can set it back to +f : set +f
when you write the script , if you wanna disable the * for example and then you want the grep to get it from a file , you do this
somewhere in the script where you want to grep the star :
set -f
# this will disable the special meaning of star , and i guess plenty of other special chars , but i don't know them
grep "*" file.txt
# here it will search for the char * just as it is , it will not expand it to any thing ..
then if you want to work with the star as a special char again ( give it the propety of expanding to take all the files ) you can set it as special char aagin by doing the reversed code which is :
Well the problem is there are lots of other special characters in that line which still need to be there. I think it's just as easy to "escape" them with the back slash ( \ ). But that's just MHO.
Have you tried it using your method? I played with it some but it didn't work.
ya i know ,
actually in this small code i worked , specially that we were not asked to go deep into the complications of what's inside the comments ( some cases could be terrible like commemts inside comments and and stars ..etc )
i don't have the code at home , i have it on the school's computer , i'll post it when i work there . the prof. said that he is gonna give us a code that deals with all the complications in this program , i'll add it too when we get it .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.