LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash User Input with Static Value Validation (https://www.linuxquestions.org/questions/linux-newbie-8/bash-user-input-with-static-value-validation-4175536702/)

powerplyer 03-14-2015 02:46 AM

Bash User Input with Static Value Validation
 
So I created this big script and want to ask for username and set a variable, however if the username exist in the username variable then continue with script otherwise ask the user to enter his name.

Flow:
-- start script "Enter User Name"
-- ask user to input username
-- if username is blank or different value then put that username in $USERNAME variable
-- then continue with the rest of the script

-- next time this user runs the script it will have his name loaded so he just presses enter to continue with the script.

-- If it is NOT the same user then, new user can erase existing input and enter a new name.

Hope it makes sense...

Thanks in advance for the help.

pan64 03-14-2015 02:51 AM

looks like a default value. You need to save settings in a file, and reload it before that question. Would be nice to see your script....

powerplyer 03-14-2015 09:53 AM

Pan64 thanks for helping. Here is the code below. I do am not familiar with creating a temp file to have the Main script use those values


Code:

UNAME="(This is where I want username to go)" #This is blank initially, but changes to a default value.
read -e -i "$UNAME" -p "Please enter your user name [enter to select default]: " input
UNAME="${input:-$UNAME}"

BTW this is one of about 5 variables I would like to user to set up initially and then have them be default values. Hope this make it clearer.

Keith Hedger 03-14-2015 10:16 AM

To start it's not a good idea to use variables that are the same as linux commands "uname" is a clicommand, it just helps with any confusion, anyway you just need to write your variables that you need to save at the end of your script ( or wherever your script exits ) like so:
Code:

echo "UNAME=\"$UNAME\"" > /tmp/savename
And read them in at the begining of your script like so
Code:

. /tmp/savename
If you need to add more than one variable use ">>" to append to the file, to clear the file to start off with just use
Code:

:> /tmp/savename
Of course you will want to write to a more permanant place than /tmp

powerplyer 03-14-2015 11:45 AM

Keith thanks for the input, I tried you methond but I am getting an error and it is not recalling the default value once set.

Here is the example code:

un.sh script name

Code:

# Set UserName
UN=. /tmp/savename
read -e -i "$UN" -p "What is you user name [Hit Enter for default]: " input
UN="${input:-$UN}"
echo "UN=\"$UN\"" > /tmp/savename

This is the Output: as you can see when, besides the error, it is not taking the new stored temp value.
Quote:

[root@John]# ./un.sh
./un.sh: line 7: /tmp/savename: Permission denied
What is you user name [Hit Enter for default]: John Doe
[root@John]# ./un.sh
./un.sh: line 7: /tmp/savename: Permission denied
What is you user name [Hit Enter for default]: # Second Run not taking the value.

Keith Hedger 03-14-2015 11:50 AM

dont't use "UN=. /tmp/savename"
just use
Code:

. /tmp/savename
the dot will include the file and so assign to the variables, you don't need to specifically assign the vars.

you can use the keyword "source" instead of "."

powerplyer 03-14-2015 12:00 PM

Thanks that helped with the error, however if I run the script a second time the default value is still blank.

Quote:

[root@John]# :> /tmp/savename --> Cleared savename
[root@John]# ./un.sh --> run script first time
What is you user name [Hit Enter for default]: john doe --> manually enter name
[root@John]# ./un.sh --> run script a second time
What is you user name [Hit Enter for default]: --> Should have John Doe populated automatically until I run a :>/temp/savename
BTW sorry for the dumb questions, I have been basic scripting for a while, but I learn something new everyday. Appreciate the quick help.

powerplyer 03-14-2015 12:13 PM

Sorry nevermind it is now working. I have commented out a line for testing and did not put it back in play. I will mark this thread solved.

Thanks again for all your help.

Keith Hedger 03-14-2015 12:15 PM

This works:
Code:

#!/bin/bash -e

#©keithhedger Sat 14 Mar 16:56:46 GMT 2015 kdhedger68713@gmail.com
# Set UserName

if [ -e /tmp/savename ];then
        . /tmp/savename
fi

read -e -i "$UN" -p "What is you user name [Hit Enter for default]: " input
UN="${input:-$UN}"
echo "UN = $UN"
echo "UN=\"$UN\"" > /tmp/savename

eg:
Code:

keithhedger@LFSStarBug:/tmp-> ./un.sh
What is you user name [Hit Enter for default]: keith
UN = keith
keithhedger@LFSStarBug:/tmp-> ./un.sh
What is you user name [Hit Enter for default]:     
UN = keith

first run typed "keith" second time just hit "enter"

Keith Hedger 03-14-2015 12:17 PM

you posted just as I did :)


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