User input
This page was last updated on April 4, 2012.
Example 1
#!/bin/bash echo "This will change to the directory you specify and list its contents."; echo; echo "Enter the path to the directory where you want to copy the files to:"; echo; read P; cd $P; echo "The contents of the directory are:"; ls;
Example 2
#!/bin/bash # This is an instructional script created by Little Girl to # show how to use input from others in scripts. It will delete # a file in a location of your choosing, so you might want to # create a junk file somwhere before running it. # # Introduce the script, letting the user know what it will do. echo "THIS SCRIPT WILL DELETE A FILE IN A LOCATION YOU SPECIFY."; echo; # Tell the user how to abort the script if they'd rather not run it. echo "PRESS CTRL+C OR CLOSE THE TERMINAL WINDOW TO ABORT THIS SCRIPT."; echo; # Ask the user for input. echo "ENTER THE PATH TO THE DIRECTORY THE FILE IS IN:"; echo; # Read the user's input. read input1; echo; # Tell the user what you're doing. echo "CHANGING TO THE DIRECTORY AND LISTING ITS CONTENTS:"; echo; # Use the input given by the user to change to that directory. cd $input1; echo; # List the contents of the current directory. ls -l; echo; # Ask the user for more input. echo "ENTER THE NAME OF THE FILE YOU'D LIKE TO DELETE HERE:"; echo; # Read the user's input. read input2; echo; # Tell the user what you're doing. echo "REMOVING $input2."; # Use the input given by the user to remove that file. rm $input2; echo; # Tell the user what you're doing. echo "VERIFYING THAT THE FILE HAS BEEN REMOVED:"; echo; # List the contents of the current directory. ls -l; # Tell the user the script is done and give the user their terminal prompt back. echo -n "DONE! PRESS THE ENTER KEY TO EXIT THIS SCRIPT."; read keypress;
Obligatory Happy Ending
And they all lived happily ever after. The end.
