Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how would you do this?

Status
Not open for further replies.

longclaw2002

Technical User
Jul 30, 2002
5
CA
Generate a report that creates two files by asking the users to choose the file names. File one reports from the current state of your Unix box on how many login sessions are currently active, who are logged on (only the usernames), and your username. File two is executable and contains a welcome program containing greeting, today's date and displaying the current directory. This file will be executed every time you log in the system. Make your report program as user friendly as possible. That is, it should interact with users effectively, and always inform the users what is going on next.


 
It looks like you need help with home work. I doubt you'll get much help here without submitting something?
 
you might be right... heres what I have... dont laugh also the alignment will be off:

echo Welcome to the super script program!
echo This program will create 2 files of your choice, please enter the first choice:
read choice1
echo great! please enter your second file name:
read choice2
echo Thank you please press enter to create the files
read enter
touch $choice1
touch $choice2
chmod 700 $choice1 $choice2
echo The files have now been created.
echo The file $choice1 will now be reporting how many sessions are running in phobos.
echo It will also report the names of those logged in and your user name.
echo to continue please press enter
read enter
echo Now we will display the information in $choice1 for you.
echo Pick 1 is you want to see the number of login sessions
echo Pick 2 if you wish to see the usernames of the people on phobos
echo Pick 3 if you wish to see your user name
echo Pick 4 when you wish to continue to the $choice2 file
echo Note! whatever choices you pick will write data to the $choice1 file!
read cpick
if [$cpick = 1]
then
echo "This is how many login sessions are currently active" >> $choice1
who| wc -l >> $choice1
who| wc -l
elif ["$cpick" -eq 2]
then
echo "This is who is logged in on phobos " >> $choice1
who|cut -c1-12 >> $choice1
who|cut -c1-12
elif ["$cpick" -eq 3]
then
echo "This is your user name" >> $choice1
whoami >> $choice1
whoami
fi

Now I tried a until statement and all I got for my trouble was a infinate loop.
 
#Hi:

# My observations:
# 1) always use a shell invocation: ksh, sh, bash, etc
# 2) place double quotes are the strings you display
# 3) always have at least one space around [ and ] in your if statements. (If
# you are using korn shell, your instructor will probably discuss using [[ and
# ]] in if statements
# 4) I'm assuming you want to loop at least once and terminate if the user
# picks 4
#
# Regards,
# ed

#!/bin/ksh

until [ "$cpick" -eq 4 ]
do
echo "Welcome to the super script program!"
echo "This program will create 2 files of your choice, please enter the first choice:"
read choice1
echo "great! please enter your second file name:"
read choice2
echo "Thank you please press enter to create the files"
read enter
touch $choice1
touch $choice2
chmod 700 $choice1 $choice2
echo "The files have now been created."
echo "The file $choice1 will now be reporting how many sessions are running in phobos."
echo "It will also report the names of those logged in and your user name."
echo "to continue please press enter"
read enter
echo "Now, we will display the information in $choice1 for you."
echo "Pick 1 is you want to see the number of login sessions"
echo "Pick 2 if you wish to see the usernames of the people on phobos"
echo "Pick 3 if you wish to see your user name"
echo "Pick 4 when you wish to continue to the $choice2 file"
echo "Note! whatever choices you pick will write data to the $choice1 file!"
read cpick
if [ "$cpick" -eq 1 ]
then
echo "This is how many login sessions are currently active" >> $choice1
who| wc -l >> $choice1
who| wc -l
elif [ "$cpick" -eq 2 ]
then
echo "This is who is logged in on phobos " >> $choice1
who|cut -c1-11 >> $choice1
who|cut -c1-11
elif [ "$cpick" -eq 3 ]
then
echo "This is your user name" >> $choice1
whoami >> $choice1
whoami
fi
done
 
Thanks, that actually help me out alot... got the loop perfect... now onto the second part...

heres what I have currently

echo Welcome to the super script program!
echo This program will create 2 files of your choice, please enter the first choice:
read choice1
echo great! please enter your second file name:
read choice2
echo Thank you please press enter to create the files
read enter
touch $choice1
touch $choice2
chmod 700 $choice1 $choice2
echo The files have now been created.
echo The file $choice1 will now be reporting how many sessions are running in phobos.
echo It will also report the names of those logged in and your user name.
echo to continue please press enter
read enter
echo Now we will display the information in $choice1 for you.
set cpick = 0
until [[ $cpick -eq 4 ]]
do
echo Pick 1 is you want to see the number of login sessions
echo Pick 2 if you wish to see the usernames of the people on phobos
echo Pick 3 if you wish to see your user name
echo Pick 4 when you wish to continue to the $choice2 file
echo Note! whatever choices you pick will write data to the $choice1 file!
read cpick
if [[ $cpick -eq 1 ]]
then
echo "This is how many login sessions are currently active" >> $choice1
who| wc -l >> $choice1
echo This is how many login sessions are currently active
who| wc -l
echo
echo please choose again
fi
if [[ $cpick -eq 2 ]]
then
echo "This is who is logged in on phobos " >> $choice1
who|cut -c1-12 >> $choice1
echo this is who is logged in on phobos
who|cut -c1-12
echo
echo please choose again
fi
if [[ $cpick -eq 3 ]]
then
echo "This is your user name" >> $choice1
whoami >> $choice1
echo this is your user name
whoami
echo
echo please choose again
fi
done

echo congratulations! You have successfully completed part 1 of the super script!
echo now onto the $choice2 file
echo press enter to continue
read enter
 
The contents of your second file could look something like this:

#!/bin/ksh

# the greeting:
# 1) perfrom a series of echos - boring!
# echo "Welcome to the system"

# use the banner command - ostentatious
# banner "Welcome to the system"

# cat the contents of some other file like the message of the day file - not bad
# cat /etc/motd

# use a here document - the best solution I think.
# The here document is terminated by the second MSG

cat << MSG

Welcome to the system! it's easy to add text

MSG

# those are back-tis - not single quotes
echo &quot;todays date is: &quot;`date`

echo &quot;the present working directory is: &quot;`pwd`
 
I have something along those lines...


echo Press enter to continue.
read enter

echo The super script will now create a welcoming greeting for you for when you login.
echo It will also display the date and the current directory.
echo


echo The super script will now check if there is already a super script: welcome,
echo date or a present working directory if there is any of these then super script
echo will insert any missing ones. If all are present then super script has been
echo run before and it is not necessary to re-run the second file.
echo

grep &quot;!WELCOME!&quot; ~/.profile |wc -l >> $welcome
grep &quot;date&quot; ~/.profile |wc -l >> $pdate
grep &quot;pwd&quot; ~/.profile |wc -l >> $ppwd


if [[ $welcome -eq 0 ]]
then
echo &quot;banner !WELCOME!&quot; >> $choice2
fi

if [[ $pdate -eq 0 ]]
then
echo &quot;date&quot; >> $choice2
fi

if [[ $ppwd -eq 0 ]]
then
echo &quot;pwd&quot; >> $choice2
fi
cat $choice2 >> ~/.profile
echo
echo Thats it! I hope you have enjoyed super script!


my new issue is that when I try to check and store the line count (using grep) it doesnt do it and instead I just get the banner !welcome! date and pwd rewritten to the end of the .profile ... once I am able to check that neither the banner message the date or the pwd is stored in the .profile file my script is finished. any thoughts on what I'm doing wrong? Im in the korn shell and im close to completion.
 
You can NOT redirect a count the way you're doing it:

grep &quot;!WELCOME!&quot; ~/.profile |wc -l >> $welcome

you have to use command line substitution:

for sh or ksh:

welcome=`grep -c &quot;!WELCOME!&quot; ~/.profile`

for ksh:

welcome=$(grep -c &quot;!WELCOME!&quot; ~/.profile)

echo $welcome

I chose to use the -c count flag of grep instead piping to wc -l.

Regards,

Ed



 
actually neither is necessary I just re-read the problem and it called for an executable file to be run everytime I log in so... my final code for the entire thing (without comments and some minor touch ups ) is:

echo Welcome to the
banner super script program!
echo This program will create 2 files of your choice.
echo Please pick file names that are not in the current directory.
echo Please pick file 1.
read choice1
echo great! please enter your second file name which is different from your first file:
read choice2
until [[ &quot;$choice1&quot; != &quot;$choice2&quot; ]]
do
echo Im sorry your files have the same name, please enter again
echo your first file name
read choice1
echo your second file name
read choice2
done

echo Thank you please press enter to create the files.
read enter
touch $choice1
touch $choice2
chmod 700 $choice1 $choice2
echo The files have now been created.
echo The file $choice1 will now be reporting how many sessions are running in phobos.
echo It will also report the names of those logged in and your user name.
echo to continue please press enter.
read enter
echo Now we will display the information in $choice1 for you.
set cpick = 0
until [[ $cpick -eq 4 ]]
do
echo Pick 1 is you want to see the number of login sessions.
echo Pick 2 if you wish to see the usernames of the people on phobos.
echo Pick 3 if you wish to see your user name.
echo Pick 4 when you wish to continue to the $choice2 file.
echo Note! whatever choices you pick will write data to the $choice1 file!
read cpick
if [[ $cpick -eq 1 ]]
then
echo &quot;This is how many login sessions are currently active&quot; >> $choice1
who| wc -l >> $choice1
echo This is how many login sessions are currently active.
who| wc -l
echo
echo please choose again.
fi
if [[ $cpick -eq 2 ]]
then
echo &quot;This is who is logged in on phobos &quot; >> $choice1
who|cut -c1-12 >> $choice1
echo this is who is logged in on phobos.
who|cut -c1-12
echo
echo please choose again.
fi
if [[ $cpick -eq 3 ]]
then
echo &quot;This is your user name&quot; >> $choice1
whoami >> $choice1
echo this is your user name.
whoami
echo
echo please choose again.
fi
done
echo
echo
echo Congratulations! You have successfully completed part 1 of the super script!
echo Now onto the $choice2 file.
echo Press enter to continue.
read enter

echo The super script will now create a welcoming greeting for you for when you login.
echo It will also display the date and the current directory.
echo
echo Before the executable file is written, if you have already preformed this script
echo or you already have a welcome greeting with the date and the current directory
echo you should not choose to continue with file 2.

echo
echo Would you like Super Script to continue with your $choice2 file?
echo Please pick 1 to continue or 2 to exit.
read choice3
if [[ $choice3 -eq 1 ]]
then
echo &quot;banner !WELCOME!&quot; >> $choice2
echo &quot;echo todays date is:&quot; >> $choice2
echo &quot;date&quot; >> $choice2
echo &quot;echo your current working directory is:&quot; >> $choice2
echo &quot;pwd&quot; >> $choice2
mv $choice2 ~
echo &quot;$choice2&quot; >> ~/.profile
fi
echo
echo Thats it! I hope you have enjoyed super script!



Thanks for your help.

chow,
longclaw2002
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top