『BASH』——Learn BashScript from Daniel Robbins——[001-002]
2013-07-26 16:40
369 查看
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
#!/bin/bash
if [ "${1##*.}" = "tar" ]
then
echo This appears to be a tarball.
else
echo At first glance, this does not appear to be a tarball.
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
#!/usr/bin/env bash
echo name of script is $0
echo first argument is $1
echo second argument is ${2}
echo seventeenth argument is ${17}
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
#!/usr/bin/env bash
#allargs.sh
for thing in "$@"
do
echo you typed ${thing}.
done
$ allargs.sh hello there you silly
you typed hello.
you typed there.
you typed you.
you typed silly.
if [[ "$myvar" -gt 3 ]]
then
echo "myvar greater than 3"
fi
if [[ "$myvar" == "3" ]]
then
echo "myvar equal 3"
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.
if [ $myvar = "foo bar oni" ]
then
echo "yes"
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:
if [ "$myvar" == "foo bar oni" ]
then
echo "yes"
fi
OR:
if [[ $myvar == "foo bar oni" ]]
then
echo "yes"
fi
The best method:
if [[ "$myvar" == "foo bar oni" ]]
then
echo "yes"
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
#!/bin/env bash
myvar=0
while [[ "$myvar" -ne 10 ]]
do
echo $myvar
myvar=$(( $myvar + 1 ))
done
#!/bin/env bash
myvar=0
until [[ $myvar -eq 10 ]]
do
echo $myvar
myvar=$(( $myvar + 1 ))
done
tarview() {
echo "Displaying contents of $@"
for x in $@
do
if [[ "${x##*.}" == "tar" ]]
then
echo "(uncompressed tar $x)"
cat $x | tar -tvf -
elif [[ "${x##*.}" == "gz" ]]
then
echo "(gzip-compressed tar $x)"
tar ztvf $x
elif [[ "${x##*.}" == "bz2" ]]
then
echo "(bzip2-compressed tar $x)"
cat $x | bunzip2 - | tar -tvf -
fi
done
}
myvar="hello"
myfunc() {
myvar="one two three"
for x in $myvar
do
echo $x > /dev/null
done
}
myfunc
echo $myvar $x
myvar="hello"
myfunc() {
local x
local myvar="one two three"
for x in $myvar
do
echo $x > /dev/null
done
}
myfunc
echo $myvar $x
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
#!/bin/bash
if [ "${1##*.}" = "tar" ]
then
echo This appears to be a tarball.
else
echo At first glance, this does not appear to be a tarball.
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
#!/usr/bin/env bash
echo name of script is $0
echo first argument is $1
echo second argument is ${2}
echo seventeenth argument is ${17}
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
#!/usr/bin/env bash
#allargs.sh
for thing in "$@"
do
echo you typed ${thing}.
done
$ allargs.sh hello there you silly
you typed hello.
you typed there.
you typed you.
you typed silly.
if [[ "$myvar" -gt 3 ]]
then
echo "myvar greater than 3"
fi
if [[ "$myvar" == "3" ]]
then
echo "myvar equal 3"
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.
if [ $myvar = "foo bar oni" ]
then
echo "yes"
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:
if [ "$myvar" == "foo bar oni" ]
then
echo "yes"
fi
OR:
if [[ $myvar == "foo bar oni" ]]
then
echo "yes"
fi
The best method:
if [[ "$myvar" == "foo bar oni" ]]
then
echo "yes"
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
#!/bin/env bash
myvar=0
while [[ "$myvar" -ne 10 ]]
do
echo $myvar
myvar=$(( $myvar + 1 ))
done
#!/bin/env bash
myvar=0
until [[ $myvar -eq 10 ]]
do
echo $myvar
myvar=$(( $myvar + 1 ))
done
tarview() {
echo "Displaying contents of $@"
for x in $@
do
if [[ "${x##*.}" == "tar" ]]
then
echo "(uncompressed tar $x)"
cat $x | tar -tvf -
elif [[ "${x##*.}" == "gz" ]]
then
echo "(gzip-compressed tar $x)"
tar ztvf $x
elif [[ "${x##*.}" == "bz2" ]]
then
echo "(bzip2-compressed tar $x)"
cat $x | bunzip2 - | tar -tvf -
fi
done
}
myvar="hello"
myfunc() {
myvar="one two three"
for x in $myvar
do
echo $x > /dev/null
done
}
myfunc
echo $myvar $x
myvar="hello"
myfunc() {
local x
local myvar="one two three"
for x in $myvar
do
echo $x > /dev/null
done
}
myfunc
echo $myvar $x
相关文章推荐
- 『BASH』——Learn BashScript from Daniel Robbins——[003]
- read line from file with bash shell script
- Bash+R: howto pass parameters from bash script to R(转)
- Bash+R: howto pass parameters from bash script to R
- [From Linux Toy] inxi– bash script to get system information
- Upgrade Bash on Ubuntu from 14.04 to 16.04
- VS2012——WebFroms UnobstrusiveValidationMode 需要“juqry”ScriptResourceMapping.
- Generate the Jobs script from msdb Database
- Learn from Social Bookmarking Sites
- Codeforces 472B. Design Tutorial: Learn from Life(坐电梯扯淡题)
- react-native 最新版本爬坑经历(unable to load script from assets 和could not connect to development server.)
- [NPM] Create a bash script to replace a complex npm script
- shell(Bash) script初学笔记
- 《learn-github-from-zero》 学习笔记
- websockets handshake using netcat/bash script
- 从开源界发现的商机——What Business Can Learn from Open Source
- 【CodeForces 472A】Design Tutorial: Learn from Math
- react-native---配置reactnative报红,run-Android时候报错unable to load script from assets 'index.android.bundl
- 【Unity】打包过程报错问题处理(UnityScriptCompilerFactory.FromCommandLineOptions)
- XML From Action Script[AS中的XML学习笔记]