IMC!


Contents


Photos

 







Browsing around...
News  News Links  Links Blog  Blog Italiano  Español 
09 - Data input in the bash

 | 

During the execution of a script you can request to the user to insert some data through the read command. This command, similarly to scanf in C, won't visualize any new message, but will wait for the user to insert a variable and press the Enter key. This is an example:

#!/bin/bash
# This script requests to the user an input from the keyboard and then visualizes it.

# The -n options avoids to wrap.
echo -n "Write your name [Enter]: "
read user_name
echo "Hello $user_name!"

 

Command line parameters
The command line parameters are variables which are passed to the script when it is launched from the command line. Inside the script these parameters can be referred as following:

VariableMeaning
$#It contains the total number of passed parameters
$num_paramContains the value of the num_param parameter
$?It contains the result of the last executed command
$0Contains the name of the script
$*Contains all the ordered parameters
"$@"Contains all the ordered parameters with double quotes


This is an example:

#!/bin/bash

# This script shows how to recall in the code the values
# passed to the script as command line parameters

clear
echo "The name of this script is: $0"
echo "You inserted $# parameters, which in order are: $*"
echo "The first one is: $1"
echo "The second one is: $2"
echo "The last executed command returned: $?"
echo
 


 | 







Comments

No comment present!

Write a comment

You can write here a comment to the article you've just read. Smiles, links and images are not allowed. The maximum comment length is 4000 characters. Please be polite, all the offensive messages will be deleted.

Your comment (lascia bianco!):
Uses (max 25 characters, required)
Web site (max 255 characters, optional)
e-Mail (max 255 characters, optional, will not be published) Your opinion (lascia bianco!):
Comment (max 4000 characters, required):





Valid HTML 4.01 Transitional
E-Mail - 156.98 ms

Valid HTML 4.01 Transitional