1 / 26

Find out yourself

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

Download Presentation

Find out yourself

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. [: =: 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

  3. 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

  4. arrays • myVal=5 • echo myVal = $myVal • i=3 • array1[$i]=$myVal • echo array1[i] = ${array1[$i]}

  5. Initializing arrays in order • array2=(x y z) • echo array2[0] = ${array2[0]} • echo array2[1] = ${array2[1]} • echo array2[2] = ${array2[2]}

  6. 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]}

  7. Elements and length of array • arrayElements=${array3[@]} • arrayLength=${#array3[@]} • echo arrayElements=\${array3[@]} $arrayElements • echo arrayLength=\${#array3[@]} $arrayLength

  8. 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}

  9. Values in a array • #tells what index have values • echo \${!array3[@]} ${!array3[@]} • ${!array3[@]}

  10. Removing an item in array • #unsets an item in the array • unset array3[1] • echo arrayLength=\${#array3[@]} $arrayLength • echo arrayElements=\${array3[@]} $arrayElements

  11. Clearing an array • #unsets all items in array (ie clears the array) • unset array3[@] • echo arrayLength=\${#array3[@]} $arrayLength • echo arrayElements=\${array3[@]} $arrayElements

  12. function • function number { • echo $1 • } • echo "function is " • number "a" "b" "c"

  13. scope() • { • local x=1 • y=2 • echo "x=$x" • echo "y=$y" • } • scope • echo "x=$x" • echo "y=$y"

  14. recursive.sh • echo this is a shell • sh recursive.sh

  15. until • read ANS • until [ $ANS = 'bye' ] • do • echo you typed $ANS • echo type something • read ANS • done

  16. Exit value • echo now type \'echo \$\?\' and see what happens • exit 4

  17. ${variable} • # it is better to rename this file. • MYSTRING="start" • echo MYSTRING is my $MYSTRING • MYSTRING=${MYSTRING}end • echo MYSTRING is my $MYSTRING

  18. Variable length • VARIABLE=THIS • echo ${#VARIABLE} • VARIABLE="this is a very long variable" • echo ${#VARIABLE}

  19. 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

  20. 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

  21. Positional parameter 1 • if [ "$1" != "" ]; then • echo "Positional parameter 1 contains something" • else • echo "Positional parameter 1 is empty" • fi

  22. Positional parameter 2 • if [ $# -gt 0 ]; then • echo "Your command line contains $# arguments" • else • echo "Your command line contains no arguments" • fi

  23. 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

  24. 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?

More Related