Shell variables are not typed, meaning that you do not specify whether a variable is a string or a number (or whatever).
Variables are declared and assigned in one statement using the equal sign: for example,
counter = 0 custname = "Harold Davis"
If a string has no embedded spaces, you do not need to use quotation marks.
A $ is used to assign the contents of one variable to another: for example,
counter = $myvar
Using command-line parameters
# Display that command line _ parameter if [ $# -eq 0 ] then echo "Please provide a _ command line name!" else echo "Hello" $1 fi
To accept interactive user input:
1. | Open a text editor. |
2. | Type the following script: #! /bin/bash # Interactive user input echo "Enter your name:" read yrname echo "Is it time for tea, $yrname?" |
3. | Save the file as yrname. |
4. | At the command line, change the file to make it executable: chmod +x yrname |
5. |
Tip
By placing read statements within infinite loops, you can create menus and menu-driven applications.
Tip
You are better off programming anything other than very simple tasks in a language such as Perl than as a shell script. Longer scripts are very hard to read and debug.
Built-in Variables | |
---|---|
Representation | Meaning |
$# | Number of command-line arguments passed to the shell |
$0 | The name of the shell program |
$* | A single string made up of all the command-line arguments |
Comparison Operators | |
---|---|
Operator | Meaning |
= | String equality |
!= | String inequality |
-eq | Arithmetic equality |
-ge | Arithmetic greater than or equal to |
-le | Arithmetic less than or equal to |
-ne | Arithmetic not equal to |
-gt | Arithmetic greater than |
-lt | Arithmetic less thanf |
3.15.237.164