Find the answer to your Linux question:
Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By atreyu
hey how r ya guys ... I was trying to solve this problem but im really sure that I didnt get to the right answer yet , I posted my ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    8

    need help with this script please


    hey how r ya guys ... I was trying to solve this problem but im really sure that I didnt get to the right answer yet , I posted my attempt so if anyone would help me looking at it and give some corrections thank u


    write a script that will :
    a) given the file name last_names exists in the current directory
    b) use a loop to prompt for 5 text lines that the user can enter last name on
    c) if the name entered is not in the file then add it to the file
    ===============================
    echo -n "Enter 5 last names"
    read guess
    for guess in 1 2 3 4 5
    do
    echo -n "Enter another last time: "
    read name
    if [ "$guess" -ne "$last_name" ]
    then
    echo "$guess" >> "$last_name"
    else
    echo "ok."
    fi
    done

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    I'm not exactly sure what you're trying to do, but is this what you mean?
    Code:
    #!/bin/sh
    
    # the file containing last names
    last_name='last_name.txt'
    
    # make sure the file exists, or quit
    ! [ -f $last_name ] && echo "$last_name: File not found" && exit 1
    
    # loop thru 5 times
    for guess in 1 2 3 4 5; do
    
      # get last name from user input
      read -p  "Enter last time #$guess: " name
    
      # look in the last name text file for the name entered
      cat $last_name|grep -q ^$name$
      if [ $? -ne 0 ]; then
    
        # if not found, append the name to the file list
        echo "$name" >> "$last_name"
      else
    
        # otherwise, indicate that it was found
        echo "name $name found."
      fi
    done
    flyman likes this.

  3. #3
    Just Joined!
    Join Date
    Aug 2011
    Posts
    8
    thank you so much I got it working now ... appreciate ur help!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •