LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Linux Bash Script Help (https://www.linuxquestions.org/questions/linux-newbie-8/linux-bash-script-help-440156/)

Urlryn 04-29-2006 08:42 PM

Linux Bash Script Help
 
Hiya all....

Trying to do a simple script that outputs to a file then ends if the inputed info is correct. But if the info is incorrect they have to go back to the start of the script and remove the file and create a new blank one. This is the part i'm having a bit of trouble

Code:

#!/bin/bash

touch user_profiles.txt
echo "Please enter you full name =>"
read full_name > user_profiles.txt
clear

echo "Please enter you street address =>"
read number street street_name >> user_profiles.txt
clear

echo "Please enter your City =>"
read city >> user_profiles.txt
clear

echo "Please enter your State (enter entire name no abbreviations) =>"
read state >> user_profiles.txt
clear

echo "Please enter your zip code =>"
read zip >> user_profiles.txt
clear

echo $full_name
echo $number $street $streetname
echo $city, $state $zip

echo "Is this correct? (please enter 1 for yes and 0 for no =>"

read correct
        if [ $correct -eq 0 ]; then
                echo "Thank you for your time, Have a nice day."
        else
                echo "`rm user_profile` ; `bash info`"
        fi

This is what i got so far....please be gentle =)

burninGpi 04-29-2006 10:41 PM

I debugged your code and made it more modular.
this should work for you.
Code:

#!/bin/bash

file=user_profiles.txt
echo -n > user_profiles.txt
clear

getvar(){                        # getvar prompt var
  echo $1
  read $2
  clear
}
getvar "Please enter you full name =>" full_name
echo $full_name >> $file
getvar "Please enter you street address =>" address
echo $address >> $file
getvar "Please enter your City =>" city
echo $city >> $file
getvar "Please enter your State (enter entire name no abbreviations) =>" state
echo $state >> $file
getvar "Please enter your zip code =>" zip
echo $zip >> $file

echo $full_name
echo $address
echo $city, $state $zip

echo "Is this correct? (y/N) =>"

read correct
        if [[ "$correct" = "Y" ]] || [[ $correct = "y" ]]; then
                echo "Thank you for your time, Have a nice day."
        else
                echo "removing user_profile.txt..." ; sleep 3s
                exec $0
        fi


Urlryn 04-29-2006 11:45 PM

Ohhh interesting...thank you very much!

Urlryn


All times are GMT -5. The time now is 10:34 PM.