• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Urban Penguin

The Urban Penguin - Linux Training

  • Home
  • About
  • Live Training
  • Shop
  • Our Latest eBook Offer
  • Programming
    • Master Editing Text Files Using VIM
    • Learn Shell Scripting with BASH
    • PERL Scripting in Linux
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • Learn C Programming using Linux and the Raspberry Pi
    • General Java Tutorials
    • Java 7 OCA Exam 1ZO-803
  • OS Tutorials
    • Red Hat and CentOS Training
      • RHCE – EX294 – Automation With Ansible
    • Learning Ubuntu
    • LPI Training
      • LPI Linux Essentials
      • LPIC-1 Linux Administrator
      • LPIC-2 Certified Linux Engineer
      • LPIC-3 Senior Level Certification
        • LPIC-3 Exam 300 : Mixed Environments
        • LPIC-3 Exam 303 : Security
        • LPIC-3 Exam 304 : Virtualization and High Availability
    • Linux Technologies
      • Apache HTTPD Server
      • Learning PHP
      • Learning PUPPET
      • Learning SAMBA
      • Linux File-Systems
      • Monitoring with Nagios Core
      • MYSQL
      • openLDAP Directories on Linux
  • Pluralsight Courses
You are here: Home / Scripting / Bash Scripting / Gather User Input

Gather User Input

July 21, 2016 by The Urban Penguin

Gather User Input in Your BASH Shell Script

21-07-2016-17-16-42Welcome to my blog how we can gather user input within our BASH shell scripts. We will be making great use of the built-in command read and your scripts will be awesome. We start with a quick demonstration of an example script. When the script runs we are prompted for the name of the user. We can then check to see if the use already exists before creating the user. Simple but shows read off really well.


As with everything that we can do with a shell script we can also run read direct from the command line to test. So, for example if we want to demonstrate how to gather user input from the command line:

read -p "Enter a user name: "

As we have not specified the variable name to use whatever we enter will populate the variable $REPLY. Should we want to specify out own variable name, which makes better sense especially for longer scripts:

read -p "Enter a user name: " username

Here we will populate a variable of our own choice called username in this example.

The option -p is for the prompt. Other options include

  • -s: To suppress the screen output such as for passwords
  • -n: To limit the number of characters that can be added. For example we might have -n2 where we prompt for a US state.

The script we use in the video is shown below. Note that in the if statement we use the exit command to leave the script if the user already exists:

read -p "Enter a name for the user: " username

if grep -q $username /etc/passwd
then
 echo "The user $username already exists"
 exit 1
fi
 
sudo useradd -m $username

As always we include a video which we hope you will follow.

My Book on Shell Scripting is available from good book shops and the publisher Packt.

shellscripting

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to print (Opens in new window)

Filed Under: Bash Scripting

Primary Sidebar

Newest Video

The Urban Penguin On Youtube

Live Training

Powered by Eventbrite

Categories

Pages

  • About The Urban Penguin
  • Contact Us
  • Shop
    • Basket
    • Checkout
    • My Account
  • LPI Training from The Urban Penguin
    • Live and Pluralsight hosted courses
    • Complete Linux Essentials
    • Scheduled Linux Essentials Classes
    • LPIC-3 Senior Level Certification
      • LPIC-3 Exam 300 : Mixed Environments
      • LPIC-3 Exam 303 : Security
      • LPIC-3 Exam 304 : Virtualization and High Availability
    • LPIC-2 Certified Linux Engineer
    • LPIC-1 Linux Administrator
    • LPI Linux Essentials for Raspberry Pi
    • LPI Linux Essentials
  • Operating System Tutorials
    • Linux Foundation Training
    • Solaris 11 OCA 1ZO-821
    • Learning Ubuntu
    • Learning SUSE
    • Red Hat and CentOS Training
      • RHCE – EX294 – Automation With Ansible
  • Scripting – the power of repetition!
    • Java 7 OCA Exam 1ZO-803
    • General Java Tutorials
    • Learn C Programming using Linux and the Raspberry Pi
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • PERL Scripting in Linux
    • Learn Shell Scripting with BASH
    • Master Editing Text Files Using VIM
  • Linux Technologies
    • Learning PUPPET
    • openLDAP Directories on Linux
    • Monitoring with Nagios Core
    • Linux File-Systems
    • Learning SAMBA
    • Apache HTTPD Server
    • Learning PHP
    • MYSQL
  • OpenStack
    • Pluralsight
    • Udemy
    • Raspberry Pi Tutorials
    • Citrix Videos
  • Courses

© 2019 The Urban Penguin · All Rights Reserved

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok