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. |
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....
|
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. |
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 Code:
. /tmp/savename Code:
:> /tmp/savename |
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 Quote:
|
dont't use "UN=. /tmp/savename"
just use Code:
. /tmp/savename you can use the keyword "source" instead of "." |
Thanks that helped with the error, however if I run the script a second time the default value is still blank.
Quote:
|
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. |
This works:
Code:
#!/bin/bash -e Code:
keithhedger@LFSStarBug:/tmp-> ./un.sh |
you posted just as I did :)
|
All times are GMT -5. The time now is 10:25 PM. |