260 likes | 379 Vues
This shell script demonstrates a comprehensive approach to password validation, handling incorrect attempts, and managing arrays. The script allows users to input a password with limited attempts, providing feedback on correctness. It also showcases features like array initialization, manipulation, and functions which include scope examples and conditional statements. The code is structured to facilitate understanding of key shell script concepts such as variable declarations, arrays, loops, and error handling.
E N D
Find out yourself • sh passwordNOTCORRECT.sh • please enter password, • you have 3 attempts • this is attempt number 1 • xyz • password NOT correct • this is attempt number 2 • pass • password correct
[: =: unary operator expected • sh passwordNOTCORRECT.sh • please enter password, • you have 3 attempts • this is attempt number 1 • passwordNOTCORRECT.sh: line 17: [: =: unary operator expected • password NOT correct • this is attempt number 2
Body of password code • echo this is attempt number $i • read PASSWORD • if • [ $PASSWORD = $data ] • then • echo password correct && ((i = 4)) • else • echo password NOT correct • fi
arrays • myVal=5 • echo myVal = $myVal • i=3 • array1[$i]=$myVal • echo array1[i] = ${array1[$i]}
Initializing arrays in order • array2=(x y z) • echo array2[0] = ${array2[0]} • echo array2[1] = ${array2[1]} • echo array2[2] = ${array2[2]}
Initializing arrays out of order • array3=([0]=a [34]=b [12]=c) • echo array3[0] = ${array3[0]} • echo array3[12] = ${array3[12]} • echo array3[34] = ${array3[34]} • echo array3[3] = ${array3[3]}
Elements and length of array • arrayElements=${array3[@]} • arrayLength=${#array3[@]} • echo arrayElements=\${array3[@]} $arrayElements • echo arrayLength=\${#array3[@]} $arrayLength
Ranges in an array • #return all from position 2 to end. • echo {array3[@]:2} ${array3[@]:2} • #returns all from position 1 and 2 • echo {array3[@]:1:2} ${array3[@]:1:2}
Values in a array • #tells what index have values • echo \${!array3[@]} ${!array3[@]} • ${!array3[@]}
Removing an item in array • #unsets an item in the array • unset array3[1] • echo arrayLength=\${#array3[@]} $arrayLength • echo arrayElements=\${array3[@]} $arrayElements
Clearing an array • #unsets all items in array (ie clears the array) • unset array3[@] • echo arrayLength=\${#array3[@]} $arrayLength • echo arrayElements=\${array3[@]} $arrayElements
function • function number { • echo $1 • } • echo "function is " • number "a" "b" "c"
scope() • { • local x=1 • y=2 • echo "x=$x" • echo "y=$y" • } • scope • echo "x=$x" • echo "y=$y"
recursive.sh • echo this is a shell • sh recursive.sh
until • read ANS • until [ $ANS = 'bye' ] • do • echo you typed $ANS • echo type something • read ANS • done
Exit value • echo now type \'echo \$\?\' and see what happens • exit 4
${variable} • # it is better to rename this file. • MYSTRING="start" • echo MYSTRING is my $MYSTRING • MYSTRING=${MYSTRING}end • echo MYSTRING is my $MYSTRING
Variable length • VARIABLE=THIS • echo ${#VARIABLE} • VARIABLE="this is a very long variable" • echo ${#VARIABLE}
Case statement • echo -n "Enter a number between 1 and 3 inclusive > " • read character • case $character in • 1 ) echo "You entered one." • ;; • 2 ) echo "You entered two." • ;; • 3 ) echo "You entered three." • ;; • * ) echo "You did not enter a number" • echo "between 1 and 3." • esac
Case statement – letter or digit • echo -n "Type a digit or a letter > " • read character • case $character in • # Check for letters • [a-z] | [A-Z] ) echo "You typed the letter $character" • ;; • # Check for digits • [0-9] ) echo "You typed the digit $character" • ;; • # Check for anything else • * ) echo "You did not type a letter or a digit" • esac
Positional parameter 1 • if [ "$1" != "" ]; then • echo "Positional parameter 1 contains something" • else • echo "Positional parameter 1 is empty" • fi
Positional parameter 2 • if [ $# -gt 0 ]; then • echo "Your command line contains $# arguments" • else • echo "Your command line contains no arguments" • fi
Positional parameter 3 • for i in $@; do • echo $i • done • for i in "$@"; do • echo $i • done • for i in $*; do • echo $i • done • for i in "$*"; do • echo $i • done
What line number • echo this is line number $LINENO • echo and this is $LINENO • Why is the useful? • What does the user care about this?