[SOLVED] Bash User Input with Static Value Validation
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150
Rep:
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
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.
Last edited by powerplyer; 03-14-2015 at 11:47 AM.
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.
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.