bash- high-level programming - variables and parameters introduced


What is variable, the variable is a method of data scripting in performance, plainly, but is variable in order to preserve the computer data entry, and a location in memory or distribution of a set of locations logo or name.

Substitution variables

Variable names is to save local variable value, a reference value of the variable is called variable substitution

$

In shellthe carefully distinguish between the name and the value of the variable variable if aa variable, it $ais a reference value of this variable, the variable data that is included.

andrew@andrew-Thurley:/work/linux-sys/bash$a=1
andrew@andrew-Thurley:/work/linux-sys/bash$ echo a 
a
andrew@andrew-Thurley:/work/linux-sys/bash$ echo $a
1

When the variable 裸体time appears, that there is no $prefixed time, then there may be situations variables

  1. Variable is declared or assigned
  2. Variables areunset
  3. Variables areexporte
  4. Or in a special case, the signal represents a variable

trap.sh

#!/bin/bash
# 使用trap来捕捉变量值.

trap 'echo Variable Listing --- a = $a b = $b' EXIT
# EXIT是脚本中exit命令所产生信号的名字.
#
# "trap"所指定的命令并不会马上执行,
#+ 只有接收到合适的信号, 这些命令才会执行.
echo "This prints before the \"trap\" --"
echo "even though the script sees the \"trap\" first."
echo
a=39
b=36
exit 0
andrew@andrew-Thurley:/work/linux-sys/bash/2.基本/src$ bash trap.sh 
This prints before the "trap" --
even though the script sees the "trap" first.

Variable Listing --- a = 39 b = 36

One pair is quoted ""enclosed variable substitution is not blocked, it is referred to as partial double quotes cited, sometimes also known as weak. If you are using single quotes, then ''it will be replaced by variable than ban, the variable name will be interpreted as a literal meaning, it does not replace the departing variable. So single quotes is called a full reference, sometimes referred to as强引用

#!/bin/bash

# 变量赋值和替换

a=375
hello=$a

#------------------------------------------------------------------
# 强烈注意, 在赋值的的时候, 等号前后一定不要有空格.
# 如果出现空格会怎么样?
 
# "VARIABLE =value"
#
#% 脚本将尝试运行一个"VARIABLE"的命令, 带着一个"=value"参数.

# "VARIABLE= value"
#
#% 脚本将尝试运行一个"value"的命令,
#+ 并且带着一个被赋值成""的环境变量"VARIABLE".
#------------------------------------------------------------------
echo hello
# 没有变量引用, 只是个hello字符串.
echo $hello
echo ${hello} # 同上.

echo "$hello"
echo "${hello}"

echo

hello="A B C D"
echo $hello
# A B C D
echo "$hello" # A B C D
# 就象你看到的echo $hello和echo "$hello"将给出不同的结果.
# ===============================================================
# 引用一个变量将保留其中的空白, 当然, 如果是变量替换就不会保留了.
# ===============================================================

echo

echo '$hello' # $hello
#
# 全引用的作用将会导致"$"被解释为单独的字符,
#+ 而不是变量前缀.

# 注意这两种引用所产生的不同的效果.

# 设置为空值.
hello=    

echo "\$hello (null value) = $hello"
# 注意设置一个变量为null, 与unset这个变量, 并不是一回事
#+ 虽然最终的结果相同(具体见下边).
 
# --------------------------------------------------------------

# 可以在同一行上设置多个变量,
#+ 但是必须以空白进行分隔.
# 慎用, 这么做会降低可读性, 并且不可移植.

var1=21 var2=22 var3=$V3
echo
echo "var1=$var1
var2=$var2
var3=$var3"
 
# 在老版本的"sh"上可能会引起问题.

# --------------------------------------------------------------

echo; echo

numbers="one two three"
#
other_numbers="1 2 3"
#

# 如果在变量中存在空白, If there is whitespace embedded within a variable,
#+ 那么就必须加上引用.
# other_numbers=1 2 3
# 给出一个错误消息.
echo "numbers = $numbers"
echo "other_numbers = $other_numbers"
# other_numbers = 1 2 3
# 不过也可以采用将空白转义的方法.
mixed_bag=2\ ---\ Whatever
#在转义符后边的空格(\).
 
echo "$mixed_bag"
# 2 --- Whatever

echo; echo

echo "uninitialized_variable = $uninitialized_variable"
# Uninitialized变量为null(就是没有值).
uninitialized_variable=
# 声明, 但是没有初始化这个变量,
 
#+ 其实和前边设置为空值的作用是一样的.
echo "uninitialized_variable = $uninitialized_variable"
 
# 还是一个空值.
 
uninitialized_variable=23
# 赋值.
unset uninitialized_variable
# Unset这个变量.
echo "uninitialized_variable = $uninitialized_variable"
 
# 还是一个空值.
echo
 
exit 0

Like C language variables, an uninitialized variable will be a nullvalue - is not an assignment (but does not represent value is 0), then use the variable before the variable assignment usually cause problems.

### Tips gas station -trap

The format trap function is to capture the signal, and signal processing

trap [-lp] [[arg] sigspec ...]

trap using the official introduction

  trap

  Automatically execute commands after receiving signals by processes or the operating system.
  Can be used to perform cleanups for interruptions by the user or other actions.

  - List available signals to set traps for:
    trap -l

  - List active traps for the current shell:
    trap -p

  - Set a trap to execute commands when one or more signals are detected:
    trap 'echo "Caught signal SIGHUP"' SIGHUP

  - Remove active traps:
    trap - SIGHUP SIGINT

  • argOr it may be a custom command shell function
  • sigspecIt may be one or more of the following
  • Defined in the < signal.hsignal name or value> in. Signal names are not case-sensitive, SIG prefix is optional. The effect of the following commands are the same
trap "echo SIGINT" SIGINT
trap "echo SIGINT" sigint
trap "echo SIGINT" 2
trap "echo SIGINT" int 
trap "echo SIGINT" Int

When debugging a script, often used in the amount of signal trap

  • EXIT: In shellfront withdraw from the trapcommand set can also be specified as 0
  • RETURN: In .and `` source 执行其他脚本返回时,执行command set trap`
  • DEBUG: Run trap disposed before any command, but only the function executes a first command before the primary function
  • ERR: when the command result is non-zero, set the trap Run
#! /bin/bash
# 使用trap实现在每个函数开始之前打印以便打印,这样就能准确的定位到函数的位置
# 从而实现对脚本的调试
trap "echo before a func is called" DEBUG
# 当. 或者 source 调用结束的时候出发
trap "echo . or source is called "  RETURN
func()
{

    echo "不管你信不信,这是一个函数"
    exit 0
}
# 测试
echo "call ."
. traptest

# 函数的调用
func
# DEBUG 查看调试信号是否有效的设置了
# trap -p RETURN
# trap -p DEBUG

exit 0

Results of the

andrew@andrew-Thurley:/work/linux-sys/bash/2.基本/src$ bash trap_func.sh 
before a func iis called
before a func iis called
call .
before a func iis called
. or source is called
before a func iis called
不管你信不信,这是一个函数
  • trap -l: Lists the names of all signals and values, similarkill -l
andrew@andrew-Thurley:~$ trap -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX	
  • trap -p: By List trap processing command signal provided through
andrew@andrew-Thurley:~$ trap -p
trap -- 'name ' SIGINT
trap -- '' SIGTSTP
trap -- '' SIGTTIN
trap -- '' SIGTTOU
trap -- 'name ' RETURN
trap "" sigspec :忽略sigspec指定的信号
trap "do something" sigspec:收到sigspec指定的信号时,do some thing后,继续执行后续命令。
trap sigspec or trap - sigspec:恢复sigspec指定的信号的默认行为

note

  • Set in a function trapalso takes effect globally of
#!/bin/bash
# trap设置在函数中的tap也是全局有效的
foo()
{    
    trap "echo func is called" DEBUG 
}

# 输出 func is called
foo
# 调试触发
#trap -p DEBUG
# 输出trap -- 'echo func is called' SIGINT
exit 0
  • For the same signal, only the last trap into force
  • trap only within this process, its children will not inherit the trap settings.

Use trap design a script execution response procedures for

#!/bin/bash
# 使用trap来捕捉变量值.

# EXIT代表在函数退出前,执行trap
trap 'echo Variable Listing --- ret=${ret}' EXIT

ps -A
if [ $? == 0 ];then
    echo "commond exec OK!"
    ret=0
else
    ret=1
fi

echo "注意这里,还没有调用trap"

# trap是在退出的时候清理程序时调用的
exit 0

Assignment of variables

=

Assignment operator (no space before and after)

note: Because =and -eqcan be used as a test operating conditions, so do not be confused with the assignment here.

=Both as a test operation conditions, it can be used for assignment, which may be required depending on the specific context

Simple variable assignment

#!/bin/bash
# "裸体"变量
echo
# 变量什么时候是"裸体"的, 比如前边少了$的时候?
# 当它被赋值的时候, 而不是被引用的时候.
# 赋值
a=879
echo "The value of \"a\" is $a."
# 使用'let'赋值
let a=16+5
echo "The value of \"a\" is now $a."
echo
# 在'for'循环中(事实上, 这是一种伪赋值):
echo -n "Values of \"a\" in the loop are: "
for a in 7 8 9 11
do
 echo -n "$a "
done
echo
echo
# 使用'read'命令进行赋值(这也是一种赋值的类型):
echo -n "Enter \"a\" "
read a
echo "The value of \"a\" is now $a."
echo

exit 0

Simple and not simply two types of variable assignment

#!/bin/bash
a=23
# 简单的赋值
echo $a
b=$a
echo $b
# 现在让我们来点小变化(命令替换).
a=`echo Hello!`
# 把'echo'命令的结果传给变量'a'
echo $a
# 注意, 如果在一个#+的命令替换结构中包含一个(!)的话,
#+ 那么在命令行下将无法工作.
#+ 因为这触发了Bash的"历史机制."
# 但是, 在脚本中使用的话, 历史功能是被禁用的, 所以就能够正常的运行.
a=`ls -l`
echo $a
echo
echo "$a"
exit 0

Using $(...)mechanisms for variable assignment (which is a method (backquotes `) update references than the rear). In fact these two
methods are a form of command substitution.

# From /etc/rc.d/rc.local
R=$(cat /etc/redhat-release)
arch=$(uname -m)

bashVariables are not case type

Unlike other program languages, bashdoes not distinguish between the type of a variable, the essence bashvariables are strings. The actual effect depends on the context, Bashalso allows the comparison operations and integer operations, one of the key factor is, whether only the value of the variable value.

#!/bin/bash
# int-or-string.sh: 整型还是字符串?

a=2334 #整型.
let "a += 1"
echo "a = $a " # a = 2335
echo  # 还是整型.
b=${a/23/BB}
# 将"23"替换成"BB".
# 这将把变量b从整型变为字符串.
echo "b = $b"
# b = BB35
declare -i b
# 即使使用declare命令也不会对此有任何帮助.
echo "b = $b"
# b = BB35
let "b += 1"
# BB35 + 1 =
echo "b = $b"
# b = 1
echo
c=BB34
echo "c = $c"
# c = BB34
d=${c/BB/23}
# 将"BB"替换成"23".
# 这使得变量$d变为一个整形.
echo "d = $d"
# d = 2334
let "d += 1"
# 2334 + 1 =
echo "d = $d"
# d = 2335
echo

# null变量会如何呢?
e=""
echo "e = $e"
# e =
let "e += 1"
# 算术操作允许一个null变量?
echo "e = $e"
# e = 1
echo
# null变量将被转换成一个整型变量.
 
# 如果没有声明变量会怎样?
echo "f = $f"
# f =
let "f += 1"
# 算术操作能通过么?
echo "f = $f"
# f = 1
echo
# 未声明的变量将转换成一个整型变量.
# 所以说Bash中的变量都是不区分类型的.
exit 0

Does not distinguish between variable type is both fortunate thing is the sad thing. It allows you the time to write the script more flexible (but adequate
enough to bring you dizzy!), And allows you to more easily write code. However, it is also prone to error, and let you develop bad
programming habits.

In this case, the programmer assumed the distinction between the script variable type of responsibility. Bash will not distinguish between the types of variables you

Special Variable Types

Local variables

This variable or function block only is visible

If a local variable is declared, then it can only be seen. This block of code is the code block office in the variable is declared in
Bu "range." In a function, a local variable in the function block only makes sense .

Environment Variables

This variable will affect the user interface and shellbehavior

Published 356 original articles · won praise 134 · views 310 000 +

Guess you like

Origin blog.csdn.net/andrewgithub/article/details/104011642