March 19, 2024, 10:13:37 AM

News : LinuxSolved.com Linux Help Community Forum..


Author Topic: Shell Scripting Help  (Read 18188 times)

Offline Chris4610

  • Linux Noob !
  • *
  • Posts: 8
Shell Scripting Help
« on: November 09, 2012, 04:18:02 AM »
Hello Everyone,

    I am looking for help in figuring out exactly what I need to do and if someone can help me figure it out, it would get me out of a huge jam. I am including what i'm being asked to do and will show an example I was given, I was told that the example could be altered to do what is asked below, I just don't understand this script stuff at all.

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.
*** READ the following paragraph to help you with your puzzle!! 
Trouble shooting your script (in case you need it)….  This script has more elements and
more potential to bonk, crash, not work.  What you should do is start with the BASIC C-S
script and start adding elements to the known to work script.  If the script bonks and
before you declare yourself “lost”(remember lost is a television show!), comment out newly
added lines until the script works again.  Now you have a block of code that needs to be
carefully reviewed.  If we were sitting in a face-to-face class this is exactly the process we
[YOU] would be asked to do.  Start with a simple script (the frame work from last week is
a good starting point) and develop it from there, don’t forget to make copies of your work
along the way!!

#! /bin/csh
echo "Enter the month date"
set a = ($<)  frugard
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


Offline Chris4610

  • Linux Noob !
  • *
  • Posts: 8
Re: Shell Scripting Help
« Reply #1 on: November 09, 2012, 06:17:58 PM »
First let me apologize for the posting I am asking for help for this one time, I have had to deal with my future mother in law and father in law almost passing away on us and it led me to fall behind a little. I have also been sick with the flu, so if anyone can please help me with this, i would be grateful. I certainly will still pose a question or 2 in the future but this would be the only time i'd ask for this much help. With my frame of mind trying to understand how to put together this long script is very hard for me, i'm trying to research it and figure it out but I have till Monday and not sure I can do it.

Ty,

Chris

Offline Chris4610

  • Linux Noob !
  • *
  • Posts: 8
Re: Shell Scripting Help
« Reply #2 on: November 12, 2012, 06:39:53 PM »
I tried to figure out how to do this long script but I can't figure it out, is there anyone at all that can help me with this?

Ty,

Chris

Offline Chris4610

  • Linux Noob !
  • *
  • Posts: 8
Re: Shell Scripting Help
« Reply #3 on: November 13, 2012, 10:31:25 PM »
I made a small error I had included 2 separate script statements thinking they were all one, the 2nd was in C-shell and did not not that. So I will post the one only for help and what i've done so far. I am getting an undefined variable, it brings up he date I want but the variable error shows and it is because of line 5. Also below what I have done so far is the script I am trying to alter into a working script, let me stress this is not a test or anything just I have been having to deal with a future mother in law and father in law that were hospitalized a week apart so I fell behind on a little of my work so I am desperate for help.

Ty,

Chris :'(

#!/bin/csh
echo "Enter the month date"
set a = ($<)
date | cut -f3 -d' '    #date command cut to the date
if ( $a == $date ) then       #if correct date entered
echo "What is my name ?" #ask for name
set b = $< #setting variable
echo "Hello $b"
else
echo "Please try again"
exit 0
endif
@ h = 0
@ i = 0
@ j = 0
@ k = 0
@ l = 0
@ z = 0
set e = /home/students/fall2012/crn12143/frugard

#! /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

Offline Chris4610

  • Linux Noob !
  • *
  • Posts: 8
Re: Shell Scripting Help
« Reply #4 on: November 16, 2012, 03:24:17 PM »
Below is something I needed help with, it is something similar to what I had gotten some help on


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