LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-08-2012, 09:12 PM   #1
Chris4610
LQ Newbie
 
Registered: Nov 2012
Posts: 5

Rep: Reputation: Disabled
Linux Script Issue


Hello, I am trying to figure this out and really could use some help. Below is what i'm being asked to do and below that is a script I had to write the previous week, according to what I need to do it's an extension to what I've already done.I have been fighting a huge cold and I really could use help in this and look forward to a reply, my email is cf4610@gmail.com.

Ty,

Chris


Case Ė switch 2, 9 points (3 if it runs correctly, 5 if all components are included and 1 for
comments) and yes we live in a partial credit world.
This project is an extension of the case and switch assignment from last week. We have
used all the elements of this script in prior homework projects, puzzles and/or scripts.
This time around everything is a choice in a case switch script.
Your shell script will use case and switch and should include the following elements:
some kind of conditional test to proceed (date, verify users is logged, etc)
request the userís name (save as a variable to be used at exit)
a counter that counts the number of total choices made by the user
a use of the cut command (cut a field from you friends list or some other file)
an option to run 5 different commands (ls, who, ping etc) and
on exiting the script, echo the userís name, user inputs (you will want to save
these as varialbes and how many choices the user made).
Comments MUST be included in the script! Your script should be easy for anyone to run
without the need of prompting from you, the developer.
Email a copy and leave an executable copy in the appropriate homework directory.
Remember, by reading your comments I should be able to see exactly what your script is
doing (or suppose to do). Why use comments? Comments force you to think through the
scripting process. Scripting could be considered a mechanical process of converting your
comments into UNIX commands.

#! /bin/csh
echo "Enter the month date"
set a = ($<)
set date = `date | cut -f 3 -d ' '` #date command cut to the date
if ( $a == $date ) then #if correct date entered
echo "What's your first name ?" #ask for name
set b = $< #setting variable
echo "Hello $b "
else
echo "I'm sorry that is not correct."
exit 0
endif
@ h = 0 # declaring the variables
@ i = 0
@ j = 0
@ k = 0
@ l = 0
@ z = 0
set e = /home/fall_05/user-name/HW/11-22 #path
set f = $e/menu #set variable for menu
while (1)
top:
clear
cat $f #cat menu
echo -n "Enter your choice "
set a = $< #set variable for choice
#set z = 1 # the counter wouldn't work until I defined the
variable
switch ( $a )
case [aA]: #case a
@ h = 1 # h is equal to 1 this sets the variable
for listing inputs
ls #command
@ z = $z + 1 #The counter
sleep 2
goto top
case [bB]:
@ i = 1
who
@ z = $z + 1
sleep 2
goto top
case [cC]:
@ j = 1 last |grep $b |wc -l
@ z = $z + 1
sleep 2
goto top
case [dD]:
@ k = 1
finger
@ z = $z + 1
sleep 2
goto top
case [eE]:
@ l = 1
@ z = $z + 1
echo "Thank you $b for entering $z choices."
echo "You ran the following commands "
if ( $h == 1 ) then
echo "ls " # if h is equal to 1 then echo ls and so
forth following down the line
if ( $i == 1 ) then
echo "who "
endif
if ( $j == 1 ) then
echo "last "
endif
if ( $k == 1 ) then
echo "finger "
endif
if ( $l == 1 ) then
echo "exit "
endif
exit 0
endsw # end switch
end # end while statement #! /bin/csh
#case switch
echo -n "Please enter your name... " #prompts user for name
set name=$< #sets user input to variable
@ i = 0 #counter begins at zero
@ a = 0
@ new = 0
@ f = 0
@ c = 0
@ n = 0
@ z = 0
@ dir = 0
@ address = 0
while (1) #while true
echo $i #counter
clear #clear screen
top: #place holder for goto
cat options #look at option file
@ i = $i + 1 #increment counter at each pass
echo options #echo options to screen
echo -n "Enter your option... " #prompts user for selection
set z=$< #sets user input to variable
switch ($z)
echo "" #inserts a blank line
case [aA] #chosen if user selects option aA
clear
echo -n "Which file would you like to copy? " #prompts user for input
set a=$< #sets user input to variable
echo -n "What is the new filename? " #prompts user for input
set new=$< #sets user input to variable
cp $a $new #copies one file to another
echo -n file $a was copied to $new #displays the file names
echo "" #inserts a blank line
goto top #return to the top and start over
case [bB] #chosen if user selects option bB
clear
echo -n "Which file would you like displayed? " #prompts user for file n
ame
set f=$< #sets user input to variable
if (-e $f) then #if the file exists then
cat $f #cat the file
endif #end if statement
echo "" #inserts a blank line
echo -n "Which column would you like to cut? " #prompts user for column
to cut set c=$< #sets user input to variable
cat $f|tr -s ' ' | cut -f $c -d ' '>duh2 #looks at file, removes spaces,
cuts field and saves as new file
echo "" #inserts a blank line
cat duh2 #look at the edited file
echo "" #inserts a blank line
goto top #return to the top and start over
case [cC] #chosen if user selects option cC
clear
echo -n "Enter the name of the file that you'd like to look at. " #promp
ts user for input
echo "" #inserts a blank line
set f=$< #sets user input to variable
cat $f #performs cat on user input
goto top #return to the top and start over
case [dD] #chosen if user selects option dD
clear #clear screen
loop: #place holder
cat options1 #look at the options1 file for additional option
s
echo options1 #echo the options to the screen
echo -n "Which function would you like to perform? "
set i=$< #sets user input to variable
switch ($i)
case [1]
clear
echo -n "Which directory would you like to look at? " #ls comman
d, prompts for directory
set dir=$< #sets user input to variable.
ls -alF $dir #performs ls on directory from user input
echo "" #inserts a blank line
goto loop #go to loop placeholder
case [2]
clear
echo "" #inserts a blank line
who #performs who
echo "" #inserts a blank line
goto loop #go to loop placeholder
case [3]
echo "" #inserts a blank line
echo -n "What is the address to ping? " #prompts user for addres
s to ping
set address=$< #sets user input to variable
ping $address -c 1 #performs ping, on user input, for one c
ount
echo "" #inserts a blank line goto loop #go to placeholder
case [4]
clear
echo "Who would you like to 'finger'?" #prompts user for name
set n=$< #sets user input to variable
finger $n #performs finger on variable
echo "" #inserts a blank line
goto loop #go to placeholder
case [5]
goto top #go to placeholder
endsw #end switch
case [eE] #chosen if user selects option eE
echo -n "You entered option e, to exit. Goodbye $name! $i options were c
hosen. Your inputs were $z, $a, $new, $f, $c, $n, $dir, $address. " #ec
ho the user's name, how many choices were made and what the inputs were.
#displays which option was entered and exits the
echo "" #inserts a blank line
exit #exit
breaksw #ends
endsw #end the switch
end #end the while statement
 
Old 11-08-2012, 09:18 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531
So, what is it exactly that you have problems with?
Show debug output, or a thorough explanation of your problem, instead of posting the assignment.
And please edit your post and put the code in BB code tags for readability.
 
Old 11-08-2012, 09:28 PM   #3
Chris4610
LQ Newbie
 
Registered: Nov 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hello, not sure if I did the BB code right, and I'm not fully sure what I'm being asked to do. What I attached was an example of what i'm being asked to do, so maybe you can help me alter it ? I never have been able to wrap my brain around this script making, so i'm desperate for help but I will try to answer any questions you have. Also my intro to programming has me doing something similar could I shoot you what I have with that to see what you think?

Tyvm,

Chris





Quote:
#!/bin/csh
echo "Enter the month date"
set a = ($<)
set date = `date | cut -f 3 -d ' '` #date command cut to the date
if ( $a == $date ) then #if correct date entered
echo "What's your first name ?" #ask for name
set b = $< #setting variable
echo "Hello $b "
else
echo "I'm sorry that is not correct."
exit 0
endif
@ h = 0 # declaring the variables
@ i = 0
@ j = 0
@ k = 0
@ l = 0
@ z = 0
set e = /home/fall_05/user-name/HW/11-22 #path
set f = $e/menu #set variable for menu
while (1)
top:
clear
cat $f #cat menu
echo -n "Enter your choice "
set a = $< #set variable for choice
#set z = 1 # the counter wouldn't work until I defined the
variable
switch ( $a )
case [aA]: #case a
@ h = 1 # h is equal to 1 this sets the variable
for listing inputs
ls #command
@ z = $z + 1 #The counter
sleep 2
goto top
case [bB]:
@ i = 1
who
@ z = $z + 1
sleep 2
goto top
case [cC]:
@ j = 1 last |grep $b |wc -l
@ z = $z + 1
sleep 2
goto top
case [dD]:
@ k = 1
finger
@ z = $z + 1
sleep 2
goto top
case [eE]:
@ l = 1
@ z = $z + 1
echo "Thank you $b for entering $z choices."
echo "You ran the following commands "
if ( $h == 1 ) then
echo "ls " # if h is equal to 1 then echo ls and so
forth following down the line
if ( $i == 1 ) then
echo "who "
endif
if ( $j == 1 ) then
echo "last "
endif
if ( $k == 1 ) then
echo "finger "
endif
if ( $l == 1 ) then
echo "exit "
endif
exit 0
endsw # end switch
end # end while statement #! /bin/csh
#case switch
echo -n "Please enter your name... " #prompts user for name
set name=$< #sets user input to variable
@ i = 0 #counter begins at zero
@ a = 0
@ new = 0
@ f = 0
@ c = 0
@ n = 0
@ z = 0
@ dir = 0
@ address = 0
while (1) #while true
echo $i #counter
clear #clear screen
top: #place holder for goto
cat options #look at option file
@ i = $i + 1 #increment counter at each pass
echo options #echo options to screen
echo -n "Enter your option... " #prompts user for selection
set z=$< #sets user input to variable
switch ($z)
echo "" #inserts a blank line
case [aA] #chosen if user selects option aA
clear
echo -n "Which file would you like to copy? " #prompts user for input
set a=$< #sets user input to variable
echo -n "What is the new filename? " #prompts user for input
set new=$< #sets user input to variable
cp $a $new #copies one file to another
echo -n file $a was copied to $new #displays the file names
echo "" #inserts a blank line
goto top #return to the top and start over
case [bB] #chosen if user selects option bB
clear
echo -n "Which file would you like displayed? " #prompts user for file n
ame
set f=$< #sets user input to variable
if (-e $f) then #if the file exists then
cat $f #cat the file
endif #end if statement
echo "" #inserts a blank line
echo -n "Which column would you like to cut? " #prompts user for column
to cut set c=$< #sets user input to variable
cat $f|tr -s ' ' | cut -f $c -d ' '>duh2 #looks at file, removes spaces,
cuts field and saves as new file
echo "" #inserts a blank line
cat duh2 #look at the edited file
echo "" #inserts a blank line
goto top #return to the top and start over
case [cC] #chosen if user selects option cC
clear
echo -n "Enter the name of the file that you'd like to look at. " #promp
ts user for input
echo "" #inserts a blank line
set f=$< #sets user input to variable
cat $f #performs cat on user input
goto top #return to the top and start over
case [dD] #chosen if user selects option dD
clear #clear screen
loop: #place holder
cat options1 #look at the options1 file for additional option
s
echo options1 #echo the options to the screen
echo -n "Which function would you like to perform? "
set i=$< #sets user input to variable
switch ($i)
case [1]
clear
echo -n "Which directory would you like to look at? " #ls comman
d, prompts for directory
set dir=$< #sets user input to variable.
ls -alF $dir #performs ls on directory from user input
echo "" #inserts a blank line
goto loop #go to loop placeholder
case [2]
clear
echo "" #inserts a blank line
who #performs who
echo "" #inserts a blank line
goto loop #go to loop placeholder
case [3]
echo "" #inserts a blank line
echo -n "What is the address to ping? " #prompts user for addres
s to ping
set address=$< #sets user input to variable
ping $address -c 1 #performs ping, on user input, for one c
ount
echo "" #inserts a blank line goto loop #go to placeholder
case [4]
clear
echo "Who would you like to 'finger'?" #prompts user for name
set n=$< #sets user input to variable
finger $n #performs finger on variable
echo "" #inserts a blank line
goto loop #go to placeholder
case [5]
goto top #go to placeholder
endsw #end switch
case [eE] #chosen if user selects option eE
echo -n "You entered option e, to exit. Goodbye $name! $i options were c
hosen. Your inputs were $z, $a, $new, $f, $c, $n, $dir, $address. " #ec
ho the user's name, how many choices were made and what the inputs were.
#displays which option was entered and exits the
echo "" #inserts a blank line
exit #exit
breaksw #ends
endsw #end the switch
end #end the while statement
[/QUOTE]

Last edited by Chris4610; 11-08-2012 at 09:29 PM.
 
Old 11-09-2012, 06:10 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531
Quote:
Originally Posted by Chris4610 View Post
not sure if I did the BB code right, and
You got that part right.


Quote:
Originally Posted by Chris4610 View Post
I'm not fully sure what I'm being asked to do.
If you don't understand your assignment then ask your teacher for clarification. If OTOH you mean what you should do here then tell us which of the 6 items on your list you work on right now, what doesn't work and why you think it doesn't work.

* Just so there'll be no misunderstanding: do not expect us provide you with a complete, grading-ready homework assignment. Like your assignments goal is to force you to think through the scripting process we try to achieve the same by asking questions. Learning takes time.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue in running commands in expect script from shell script yadvinder Programming 0 05-31-2012 05:07 AM
[SOLVED] Linux issue renaming files - BASH script giantanakim Programming 25 11-01-2011 01:21 PM
Issue with Script?? linuxshadow Linux - Newbie 1 06-11-2008 02:28 AM
Windows and Linux Server. Issue with script. DeathGoth Programming 0 10-03-2004 04:45 PM


All times are GMT -5. The time now is 08:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration