Results 1 to 10 of 16
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 11-08-2012 #1Just Joined!
- Join Date
- Nov 2012
- Posts
- 9
Scripting in BASH
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 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
- 11-09-2012 #2Just Joined!
- Join Date
- Nov 2012
- Posts
- 9
I pray that someone can help me with the script, I desperately need help for this and ty to anyone in advance.
- 11-15-2012 #3Just Joined!
- Join Date
- Nov 2012
- Posts
- 18
Code:#! /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
- 11-18-2012 #4Just Joined!
- Join Date
- Nov 2012
- Posts
- 9
Hey Uglyboy,
Thank you for your help with my other issue, I have another that I have actually tried doing myself but I swear this is like Spanish to me. Below is what i'm being asked to do and below that will be what i've attempted so far. I have been able to get a couple of the things being asked to work alone but combing them into a whole script has been a problem.
Your case and switch script should:
-request a user name,
-request 8 numbers and ask the user if they are to be added or sorted (both need to
work!) OR request 8 names (1
st
and last) and sort them by the second field (check
the hints and tips sheet for multi part user input),
-multiply two numbers from user input,
- give the user the choice of running; ‘who’, ‘w’ or ‘finger’ and echo only the first
and third columns to the screen (save this also for exit echo),
- have a conditional ‘while’ statement,
- track the number of choices the users makes (use a counter),
- have at least 2 if statements,
- include an option that will add from a start number and end at another number
using user input for all numbers including the increment number (sound familiar?).
Neatly (review echo in your book if necessary and experiment with formatting)
echo all the results (you should have them all saved as variables) and user input back to
the screen in a user friendly format (can you say ‘easy to read’?). Output on the screen
will show what commands the user executed, the user’s name, other user inputs (name,
any numbers etc), the sum of added numbers, the product of multiplied numbers and how
many choices the user made.
Remember, by reading your comments I should be able to see exactly what your script is
doing at every key point of the script and comments count! Your script should be easy for
anyone to run without the need of prompting from you the developer
Also add a that use a nested switch in their script!
#!/bin/csh
head -20 sorted_second | tail -8
sort -t"," -k 2 sorted_second
I was trying to get the first 8 names from my file & sort it by the 2nd field only could not get it.
#!/bin/bash
num=2
ps=3
ans=$((num*ps))
echo ${ans}
Here I was able to get it to multiply 2 numbers as asked but have not been able to include it properly into the script. I really could use help in this regard and would be greatly appreciated.
Ty,
Chris
- 11-19-2012 #5Just Joined!
- Join Date
- Nov 2012
- Posts
- 18
Code:#!bin/bash echo -n " Please Enter a Username : " read username if [ -z "$username" ]; then echo "No Username!" exit fi echo "Hello $username" #As For Sum #!/bin/bash num=2 ps=3 ans=$((num+ps)) echo "The Addition of $num and $ps is $ans"Last edited by uglyboy; 11-19-2012 at 05:59 AM.
- 11-19-2012 #6Just Joined!
- Join Date
- Nov 2012
- Posts
- 9
Awesome tyvm is there any chance you can help me with the rest that is being asked & I promise i'll find some way to show appreciation for this because it's bailing me out of a huge jam.
Ty,
Chris
- 11-20-2012 #7Just Joined!
- Join Date
- Nov 2012
- Posts
- 18
- 11-20-2012 #8Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,657
Hi,
This appears to be a homework question, and asking for help on them is against Forum Rules.
That does not mean that we cannot help you with an isolated issue, though (versus doing your homework for you outright). If you have a specific problem with one aspect (programming or otherwise), please focus on that issue, and please indicate what you have tried and any errors generated when run. We will attempt to give you pointers and references for where you can learn about such things.
The man pages that come with all Linux distros is usually the best place to start (and much of what you ask is covered there). Properly crafted google searches will also avail you. Also check your init scripts dir (e.g. /etc/init.d/) for examples of scripting.
For starters:
Code:man bash
Code:man csh
- 11-20-2012 #9Just Joined!
- Join Date
- Nov 2012
- Posts
- 9
Below is something that seems simple but it isn't, I listed what i have to do and below that is my attempt. It runs but it just doesn't seem right and honestly this is my first time experiencing foreach scripts...please help.
1 - Use a foreach script to list all the files in a directory (user input
needed – what dir to list) and echo the number of files in those directories.
** 2 points **
2 - Use a foreach script to change the case of the names (first, last or both
names) in your ‘friends’ database (you will need to cut out all the extra info).
** 3 points **
3 - Use a combination of scripting skills that will send user input (asked from the
command line) into a file that is [the file] then processed by the foreach script as
part of your script. The foreach will manipulate the file somehow – your choice
of manipulation!
#! /bin/csh
foreach i (HW-Oct29 HW-Nov12 HW-Nov13 week04 week05)
#the above line assigns text files to var 'i' one at a time
echo $i file #echo each variable followed by the word 'file'
end #end the foreach loop
- 11-20-2012 #10Just Joined!
- Join Date
- May 2012
- Posts
- 43
Any command that will contain spaces needs to have the content wrapped in quotes, otherwise it reads them as seperate commands.
Just change this line so it looks like this.
Code:echo "$i file"
Also, homework is against the forum rules. So we shouldn't be solving it for you, just pointing you in the right direction.


Reply With Quote

