Results 1 to 3 of 3

Thread: Using read to prompt for editable user input in Bash 3

  1. #1
    Junior Member
    Join Date
    Dec 2008
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Using read to prompt for editable user input in Bash 3

    Below is a simple script to prompt for user input while suggesting an editable default value at the prompt:


    Code:
    shortname=user1
    
    read -e -i $shortname -p "Please enter the username you would like to add: " input
    USERNAME="${input:-$shortname}"
    
    Please enter the username you would like to add: user1
    The above is the expected and working output in BASH 4

    If I try this same code in BASH 3 it doesn't work because this version does not support the "-i" switch.

    I need some help getting the same result in BASH 3.
    Thanks

  2. #2
    Senior Member
    Join Date
    Aug 2011
    Posts
    371
    Thanks
    0
    Thanked 55 Times in 51 Posts
    Rep Power
    7

    Default

    hi,

    if this feature doesn't exist, you can't!

    an answer is already in your code:
    Code:
    shortname=user1
    
    read -e -p "Please enter the username you would like to add: (default == $shortname) " input
    USERNAME="${input:-$shortname}"
    if $input doesn't exist, or is empty, then its default is $shortname's value.
    «A problem clearly stated is a problem half solved.»

  3. #3
    Junior Member
    Join Date
    Dec 2008
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by Watael View Post
    hi,

    if this feature doesn't exist, you can't!

    an answer is already in your code:
    Code:
    shortname=user1
    
    read -e -p "Please enter the username you would like to add: (default == $shortname) " input
    USERNAME="${input:-$shortname}"
    if $input doesn't exist, or is empty, then its default is $shortname's value.

    Typically in scripting there are several ways to skin a cat. I understand there is a bash limitation when trying to do it "my way". I was hoping someone might be able to show me another way to accomplish the same goal in bash 3.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. script to read passwd file from input
    By Razz0lyne in forum Shell scripting
    Replies: 1
    Last Post: 27th November 2012, 03:41 AM
  2. /dev/sdj: read failed after 0 of 4096 at 0: Input/output error
    By Naveen Joshi in forum CentOS / RHEL / Fedora
    Replies: 0
    Last Post: 24th February 2011, 08:51 PM
  3. Replies: 3
    Last Post: 1st November 2010, 03:43 PM
  4. Add Color To MAC OSX Bash Prompt
    By jaysunn in forum Getting started tutorials
    Replies: 3
    Last Post: 16th December 2009, 04:17 PM
  5. Bash ps1 prompt
    By chinalinix in forum Shell scripting
    Replies: 2
    Last Post: 18th July 2009, 12:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41