Ubuntu: Shell script with user input prompt



Question:

Is it possible to write a script where the initial command requires user input so that it prompts the user to enter that input before running?

Example:

sudo add-apt-repository ppa:"this"/"and_that"  


Solution:1

You can read user input in bash the following way:

read -p "Input this: " this  read -p "Input that: " that  sudo add-apt-repository ppa:${this}/${that}  

read command creates a variable with its value taken from standard input.

In real life scenario you should also sanitize user input (i.e. check for non-alphanumeric characters before calling the command), but you can skip it if it's just for you.


Solution:2

You can use command read to read user input into variables.

#!/bin/bash    echo Enter this:  read this    echo and that:  read and_that    sudo add-apt-repository ppa:$this/$and_that  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »