Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 75
How to put the default value

Hi guys
Iam looking for a small help.I wrote a script. in that iam trying to take the default value ,when we press enter.this part iam struggling can somebody help please

Example:

do u need this server[dingdong.xxx.com]:

For this one if we press enter it has to take server name as dingdong.xxx.com.

Can somebody help me with this.Appreciate your help guys

--CoolKid

Last edited by coolkid; 05-23-2008 at 04:47 PM..
Sponsored Links
  #2 (permalink)  
Old 05-23-2008
DukeNuke2's Avatar
Soulman
 

Join Date: Jul 2006
Location: Germany, Berlin
Posts: 3,287
you can check if the variable is empty. if it is empty, set the needed value and you are done...

something like:


Code:
read var
if [ -z $var ]
then
 var=your value
fi

  #3 (permalink)  
Old 05-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 75
Nice...It works as expected thanks a lot bud
  #4 (permalink)  
Old 05-23-2008
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 3,216
You can use the following syntax:


Code:
$ ./s
do u need this server[dingdong.xxx.com]:
server is: dingdong.xxx.com
$ ./s
do u need this server[dingdong.xxx.com]: another
server is: another
$ cat s
#! /bin/bash

read -p  "do u need this server[dingdong.xxx.com]: " server
printf "server is: ${server:=dingdong.xxx.com}\n"

For more info check the man pages of your shell,
for example:


Code:
man bash|less -p:=


Code:

     ${parameter:=word}    Assign Default Values.  If parameter is unset or
                           null, the expansion of word is assigned to parame‐
                           ter.  In all cases, the final value of parameter is
                           substituted.  Only variables, not positional param‐
                           eters or special parameters, can be assigned in
                           this way.

  #5 (permalink)  
Old 05-23-2008
DukeNuke2's Avatar
Soulman
 

Join Date: Jul 2006
Location: Germany, Berlin
Posts: 3,287
nice one... have to remember this!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




All times are GMT -4. The time now is 04:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0