LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2010, 08:56 AM   #1
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Rep: Reputation: 16
bash script 'read' with default values


I have a fallowing simple bash script:
Code:
#!/bin/bash

echo -n "Please insert your name: "
read NAME

echo -n "Please enter your sex(m or f, default is m): "
read SEX

echo -n "Please enter your hometown(default is New-York): "
read TOWN
echo
echo
echo name: $NAME
echo sex: $SEX
echo hometown: $TOWN
It works fine, but is it possible to have default values using bash read(1b)? I mean if user is prompter for sex, he just pushes ENTER and by default 'm' is chosen. Or if user is prompted for hometown and he doesn't insert anything, but just pushes ENTER button, 'New-York' sis chosen by default Are such default values possible in bash?

Last edited by m4rtin; 03-05-2010 at 08:59 AM.
 
Old 03-05-2010, 09:06 AM   #2
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
Yes, after 'read SEX' add the line 'SEX="${SEX:=m}"' and after
'read TOWN' add 'TOWN=$"{TOWN:=New-York}"'

This syntax means to set the variable to its current value,
unless it is empty, in which case it will take the new value.
 
  


Reply



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
Bash script is enterpreting $1, $2 values in awk script ... praveen_218 Programming 4 09-14-2009 03:38 PM
bash script--variables have unexpected values on invoking mplayer stairwayoflight Programming 5 07-08-2007 11:17 AM
Matching values in a bash script grep, regex's ... ? maxvonseibold Linux - General 6 01-29-2007 06:07 AM
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 05:23 PM
(Bash) Saving READ values in external .conf files spikylee Linux - Newbie 4 10-28-2003 06:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:03 PM.

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
Open Source Consulting | Domain Registration