『BASH』——Learn BashScript from Daniel Robbins——[001-002]

ABSTRACT:

  Daniel Robbins is best known as the creator of Gentoo Linux and author of many IBM developerWorks articles about Linux. Daniel currently serves as Benevolent Dictator for Life (BDFL) of Funtoo Linux. Funtoo Linux is a Gentoo-based distribution and continuation of Daniel's original Gentoo vision.


Section 1


$ myvar='This is my environment variable!'

$ echo $myvar

This is my environment variable!

$ echo foo$myvarbar

foo

$ echo foo${myvar}bar

fooThis is my environment variable!bar

$ echo foo"${myvar}"bar

fooThis is my environment variable!bar

$ echo foo"$myvar"bar

fooThis is my environment variable!bar

$ basename /usr/local/share/doc/foo/foo.txt

foo.txt

$ basename /usr/home/drobbins

drobbins

$ dirname /usr/local/share/doc/foo/foo.txt

/usr/local/share/doc/foo

$ dirname /usr/home/drobbins/

/usr/home

$ MYFILES=$(ls /etc | grep pa)

$ echo $MYFILES

pam.d passwd

$ MYFILES=$(ls $(dirname foo/bar/oni))

  NOTE:$( ) is generally preferred over ` ` in shell scripts,because it is more universally supported across different shells,it is less complicated to use in a nested form.

$ MYVAR=foodforthought.jpg

$ echo ${MYVAR##*fo}

rthought.jpg

$ echo ${MYVAR#*fo}

odforthought.jpg

$ MYFOO="chickensoup.tar.gz"

$ echo ${MYFOO%%.*}

chickensoup

$ echo ${MYFOO%.*}

chickensoup.tar

$ MYFOOD="chickensoup"

$ echo ${MYFOOD%%soup}

chicken

  • $ MYFOOD="soupchickensoup"
  • $ echo ${MYFOOD%soup}
  • soupchicken
  • $ echo ${MYFOOD%%soup}
  • soupchicken

$ EXCLAIM=cowabunga

$ echo ${EXCLAIM:0:3}

cow

$ echo ${EXCLAIM:3:5}

abung 

  • $ echo ${EXCLAIM:3}
  • abunga

  NOTE:standard format is ${VAR:offset:length},if there is no ":length" given,then default to the end of the original variable

$ MYFOOD="chickensoup and another dogsoup"

$ echo ${MYFOOD/soup/rubbish}

chickenrubbish and another dogsoup

$ echo ${MYFOOD//soup/rubbish}

chickenrubbish and another dogrubbish

  1. #!/bin/bash  
  2. if [ "${1##*.}" = "tar" ]
  3. then
  4.    echo This appears to be a tarball.
  5. else
  6.    echo At first glance, this does not appear to be a tarball.
  7. fi

$ ./mytar.sh thisfile.tar

This appears to be a tarball.

$ ./mytar.sh thatfile.gz

At first glance, this does not appear to be a tarball.


Section 2


  1. #!/usr/bin/env bash
  2.   echo name of script is $0
  3.   echo first argument is $1
  4.   echo second argument is ${2}
  5.   echo seventeenth argument is ${17}
  6.   echo number of arguments is $#

  NOTE:bash features the "$@" variable, which expands to all command-line parameters separated by spaces.
  NOTE:“$*”,not be separated,as one new parameter 

  1. #!/usr/bin/env bash
  2. #allargs.sh  
  3. for thing in "$@"
  4. do
  5.   echo you typed ${thing}.
  6. done

$ allargs.sh hello there you silly

you typed hello. you typed there. you typed you. you typed silly.

  1. if [[ "$myvar" -gt 3 ]]
  2. then
  3.    echo "myvar greater than 3"
  4. fi  
  1. if [[ "$myvar" == "3" ]]
  2. then
  3.   echo "myvar equal 3"
  4. fi

  NOTE:In the above two comparisons do exactly the same thing, but the first uses arithmetic comparison operators, while the second uses string comparison operators.

  NOTE:the second can't be used to [[ "$myvar" > "10" ]],because "2" is larger than "10" in the comparision of string.  

  1. if [ $myvar = "foo bar oni" ]
  2. then
  3.    echo "yes"
  4. fi  output  [: too many arguments

  NOTE:In this case, the spaces in "$myvar" (which equals "foo bar oni") end up confusing bash. After bash expands "$myvar", it ends up with the following comparison:[ foo bar oni = "foo bar oni" ].

  Because the environment variable wasn't placed inside double quotes, bash thinks that you stuffed too many arguments in-between the square brackets. You can easily eliminate this problem by surrounding the string arguments with double-quotes or use double square brackets. Remember, if you get into the habit of surrounding all string arguments and environment variables with double-quotes, you'll eliminate many similar programming errors. Here's how the "foo bar oni" comparison should have been written:

  1. if [ "$myvar" == "foo bar oni" ]
  2. then
  3.   echo "yes"
  4. fi

OR:

  1. if [$myvar == "foo bar oni" ]]
  2. then
  3.   echo "yes"
  4. fi

The best method:

  1. if [[ "$myvar" == "foo bar oni" ]]
  2. then
  3.   echo "yes"
  4. fi  SO!!! use [[ ]] instead of [ ]

$ echo $(( 100 / 3 ))

33

$ myvar="56"

$ echo $(( $myvar + 12 ))

68

$ echo

$(( $myvar - $myvar ))

0

$ myvar=$(( $myvar + 1 ))

$ echo $myvar

57

  1. #!/bin/env bash
  2. myvar=0
  3. while [[ "$myvar" -ne 10 ]]
  4.   do
  5.   echo $myvar
  6.   myvar=$(( $myvar + 1 ))
  7. done
  1. #!/bin/env bash
  2. myvar=0
  3. until [[ $myvar -eq 10 ]]
  4. do
  5.   echo $myvar
  6.   myvar=$(( $myvar + 1 ))
  7. done
  1. tarview() {
  2.   echo "Displaying contents of $@"
  3.   for x in $@
  4.     do
  5.     if [[ "${x##*.}" == "tar" ]]
  6.     then
  7.       echo "(uncompressed tar $x)"
  8.       cat $x | tar -tvf -
  9.     elif [[ "${x##*.}" == "gz" ]]
  10.     then
  11.       echo "(gzip-compressed tar $x)"
  12.       tar ztvf $x
  13.     elif [[ "${x##*.}" == "bz2" ]]
  14.     then
  15.       echo "(bzip2-compressed tar $x)"
  16.       cat $x | bunzip2 - | tar -tvf -
  17.     fi
  18.   done
  19. }
  1. myvar="hello"
  2. myfunc() {
  3.   myvar="one two three"
  4.   for x in $myvar
  5.   do
  6.     echo $x > /dev/null
  7.   done
  8. }
  9. myfunc
  10. echo $myvar $x
  1. myvar="hello"
  2. myfunc() {
  3.   local x  
  4.   local myvar="one two three"
  5.   for x in $myvar
  6.   do
  7.     echo $x > /dev/null
  8.   done
  9. }
  10. myfunc
  11. echo $myvar $x
原文地址:https://www.cnblogs.com/hadex/p/5705095.html