LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-14-2015, 03:46 AM   #1
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Rep: Reputation: Disabled
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.
 
Old 03-14-2015, 03:51 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,505

Rep: Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434Reputation: 2434
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....
 
Old 03-14-2015, 10:53 AM   #3
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-14-2015, 11:16 AM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,307

Rep: Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566
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
 
Old 03-14-2015, 12:45 PM   #5
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Original Poster
Rep: Reputation: Disabled
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 12:47 PM.
 
Old 03-14-2015, 12:50 PM   #6
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,307

Rep: Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566
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 "."
 
Old 03-14-2015, 01:00 PM   #7
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-14-2015, 01:13 PM   #8
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-14-2015, 01:15 PM   #9
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,307

Rep: Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566
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"
 
Old 03-14-2015, 01:17 PM   #10
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,307

Rep: Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566
you posted just as I did
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 03:57 PM
[SOLVED] Bash user input huntaz556 Linux - Newbie 14 10-24-2011 12:13 PM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 07:40 PM
Bash Y/N user input zcrxsir88 Programming 11 04-16-2008 12:35 PM
Bash, input validation: request for comments unSpawn Programming 3 07-25-2003 09:03 PM


All times are GMT -5. The time now is 07:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration