bash$ echo *
abs-book.sgml add-drive.sh agram.sh alias.sh
-算术运算中表示乘法。
表示条件测试;
在双括号内表示C风格的三元操作符
(( var0 = var1<98?9:21 ))
# ^ ^
# if [ "$var1" -lt 98 ]
# then
# var0=9
# else
# var0=21
# fi
#!/bin/bash
# Check some of the system's environmental variables.
# This is good preventative maintenance.
# If, for example, $USER, the name of the person at the console, is not set,
#+ the machine will not recognize you.
: ${HOSTNAME?} ${USER?} ${HOME?} ${MAIL?}
echo
echo "Name of the machine is $HOSTNAME."
echo "You are $USER."
echo "Your home directory is $HOME."
echo "Your mail INBOX is located in $MAIL."
echo
echo "If you are reading this message,"
echo "critical environmental variables have been set."
echo
echo
作为通配符,用于匹配文件名扩展特性中,用于匹配单个字符;
正则表达式中,表示匹配其前面规则0次或者1次。
var1=5
var2=23skidoo
echo $var1 # 5
echo $var2 # 23skidoo
your_id=${USER}-on-${HOSTNAME}
echo "$your_id"
#
echo "Old \$PATH = $PATH"
PATH=${PATH}:/opt/bin # Add /opt/bin to $PATH for duration of script.
echo "New \$PATH = $PATH"
quote=$'\042'.
位置参数,这个在使用脚本文件的时候,在传递参数的时候会用到,两者都能返回调用脚本文件的所有参数
$* 是将所有参数作为一个整体返回(字符串)
$@是将每个参数作为单元返回一个参数列表
index=1 # Initialize count.
echo "Listing args with \"\$*\":"
for arg in "$*" # Doesn't work properly if "$*" isn't quoted.
do
echo "Arg #$index = $arg"
let "index+=1"
done # $* sees all arguments as single word.
echo "Entire arg list seen as single word."
echo
index=1 # Reset count.
# What happens if you forget to do this?
echo "Listing args with \"\$@\":"
for arg in "$@"
do
echo "Arg #$index = $arg"
let "index+=1"
done # $@ sees arguments as separate words.
echo "Arg list seen as separate words."
echo
index=1 # Reset count.
echo "Listing args with \$* (unquoted):"
for arg in $*
do
echo "Arg #$index = $arg"
let "index+=1"
done # Unquoted $* sees arguments as separate words.
echo "Arg list seen as separate words."
exit 0
a=123
( a=321; )
echo "a = $a" # a = 123
# "a" within parentheses acts like a local variable.
Array=(element1 element2 element3)
echo \"{These,words,are,quoted}\" # " prefix and suffix
# "These" "words" "are" "quoted"
cat {file1,file2,file3} > combined_file
# Concatenates the files file1, file2, and file3 into combined_file.
cp file22.{txt,backup}
# Copies "file22.txt" to "file22.backup
#!/bin/bash
echo {a..z} # a b c d e f g h i j k l m n o p q r s t u v w x y z
# Echoes characters between a and z.
echo {0..3} # 0 1 2 3
# Echoes characters between 0 and 3.
base64_charset=( {A..Z} {a..z} {0..9} + / = )
# Initializing an array, using extended brace expansion.
# From vladz's "base64.sh" example script.
#!/bin/bash
a=123
{ a=321; }
echo "a = $a" # a = 321 (value inside code block)
# Thanks, S.C.
#!/bin/bash
# Reading lines in /etc/fstab.
File=/etc/fstab
{
read line1
read line2
} < $File
echo "First line in $File is:"
echo "$line1"
echo
echo "Second line in $File is:"
echo "$line2"
exit 0
# Now, how do you parse the separate fields of each line?
# Hint: use awk, or . . .
# . . . Hans-Joerg Diers suggests using the "set" Bash builtin.
#!/bin/bash
# copydir.sh
# Copy (verbose) all files in current directory ($PWD)
#+ to directory specified on command-line.
E_NOARGS=85
if [ -z "$1" ] # Exit if no argument given.
then
echo "Usage: `basename $0` directory-to-copy-to"
exit $E_NOARGS
fi
ls . | xargs -i -t cp ./{} $1
# ^^ ^^ ^^
# -t is "verbose" (output command-line to stderr) option.
# -i is "replace strings" option.
# {} is a placeholder for output text.
# This is similar to the use of a curly-bracket pair in "find."
#
# List the files in current directory (ls .),
#+ pass the output of "ls" as arguments to "xargs" (-i -t options),
#+ then copy (cp) these arguments ({}) to new directory ($1).
#
# The net result is the exact equivalent of
#+ cp * $1
#+ unless any of the filenames has embedded "whitespace" characters.
exit 0
find ~/ -name 'core*' -exec rm {} \;
# Removes all core dump files from user's home directory.
测试的表示 Shell会测试在[]内的表达式
在数组的上下文中,表示数组元素,方括号内填上数组元素的位置就能获得对应位置的内容
Array[1]=xxx
echo ${Array[1]};
"[xyz]" matches any one of the characters x, y, or z.
"[c-n]" matches any one of the characters in the range c to n.
"[B-Pk-y]" matches any one of the characters in the ranges B to P and k to y.
"[a-z0-9]" matches any single lowercase letter or any digit.
Combined sequences of bracketed characters match common word patterns. "[Yy][Ee][Ss]" matches yes, Yes, YES, yEs, and so forth. "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" matches any Social Security number.
"[^b-d]" matches any character except those in the range b to d. This is an instance of ^ negating(否定) or inverting the meaning of the following RE (taking on a role similar to ! in a different context).
# [[ Octal and hexadecimal evaluation ]]
# Thank you, Moritz Gronbach, for pointing this out.
decimal=15
octal=017 # = 15 (decimal)
hex=0x0f # = 15 (decimal)
if [ "$decimal" -eq "$octal" ]
then
echo "$decimal equals $octal"
else
echo "$decimal is not equal to $octal" # 15 is not equal to 017
fi # Doesn't evaluate within [ single brackets ]!
if [[ "$decimal" -eq "$octal" ]]
then
echo "$decimal equals $octal" # 15 equals 017
else
echo "$decimal is not equal to $octal"
fi # Evaluates within [[ double brackets ]]!
if [[ "$decimal" -eq "$hex" ]]
then
echo "$decimal equals $hex" # 15 equals 0x0f
else
echo "$decimal is not equal to $hex"
fi # [[ $hexadecimal ]] also evaluates!
a=3
b=7
echo $[$a+$b] # 10
echo $[$a*$b] # 21
#!/bin/bash
# c-vars.sh
# Manipulating a variable, C-style, using the (( ... )) construct.
echo
(( a = 23 )) # Setting a value, C-style,
#+ with spaces on both sides of the "=".
echo "a (initial value) = $a" # 23
(( a++ )) # Post-increment 'a', C-style.
echo "a (after a++) = $a" # 24
(( a-- )) # Post-decrement 'a', C-style.
echo "a (after a--) = $a" # 23
(( ++a )) # Pre-increment 'a', C-style.
echo "a (after ++a) = $a" # 24
(( --a )) # Pre-decrement 'a', C-style.
echo "a (after --a) = $a" # 23
echo
echo
(( t = a<45?7:11 )) # C-style trinary operator.
# ^ ^ ^
echo "If a < 45, then t = 7, else t = 11." # a = 23
echo "t = $t " # t = 7
echo