CP5 Basic Scripting : Shell Variables

variables ——containers that hold strings and numbers,which can be changed,compared,and passed around.

bash variables have some very special operators that can be used when you refer to the vairable.

variables in a bash script are often written as all-uppercase names,though that is not required---just a common practice.

1.Documenting Your Script

Problem: you need to be able to understand your script even when several months have passed since you wrote it.

Solution: Document your script with comments. The # character denotes the beginning of a comment.All the characters after it on that line are ignored by the shell.

2.Embedding Documentation in Shell Scripts

Problem: you You want a simple way to provide end-user documentation for your script.You want to keep both code and documentation mark up in the same file to simplify updates,distribution, and revision control.

Solution: Embed documentation in the script using the "do nothing" built-in(a colon) and a here-document.

 

 

3.Promoting Script Readability

Problem: You'd like to make your script as readable as possible for ease of understanding and future maintenance.

Solution:  

  •       Indent and use vertical whitespace wisely
  • Use meaningful variable names
  • Use functions,and give them meaningful names
  • Break lines at meaningful places at less than 76 character or so
  • Put the most meaningful bits to the left

5.Exporting Variables

Problem: you defined a variable in one script,but when you called another script it didn't know about the variable

Solution: Export variables that you want to pass on to other scripts

6.Seeing All Variable Values

Problem: How can I see which variables have been exported and what values they have?Do I have to echo each one by hand?How would I tell if they are exported.

Solution: Use the set command to see the value of all variables and function definitions in the current shell. Use the env (or export -p) command to see only those variables that have been exported and would be available to a subshell.

 

7.Using Parameters in a Shell Script

Problem: You also want users to be able to invoke your script with a parameter.You could require that users set a shell variable,but that seems clunky.You also need to pass data to another script.You could agree on environment variables,but that ties the two scripts together too closely.

Solution: Use command-line parameters.Any words put on the command line of a shell script are available to the script as numbered variables:

8.Looping Over Arguments Passed to a Script

Problem: You want to take some set of actions for a given list of arguments.You could write your shell script to do that for one argument and use $1 to reference the parameter.But what if you'd like to do this for a whole buhch of files?You would like to be able to invoke your script like this:

actall   *.txt

Solution: Use the shell special variable $* to refer to all of your arguments, and use that in a for loop like thi:

 

9.Handling Parameters with Blanks

Problem: You wrote a script that took a filename as a parameter  and it seemed to work,but then one time your script failed.The filename,it turns out,had an embedded blank.

Solution: You'll need to be careful to quote any shell parameters that might contain filenames,When referring to a variable,put the variable reference inside double quotes.

11.Counting Arguments

Problem: You need to know with how many parameters the script was invoked.

Solution: Use the shell built-in variable ${#}

 

11.Consuming Arguments

Problem: For any serious shell script,you are likely to have two kinds of arguments--options that modify the behavior of he script and the real arguments with which you want to work.

Solution: 

 

12.Getting Default Values

Problem: You have a shell script that takes arguments supplied on the command line.You'd like to provide default values so that the most common values can be used without needing to type them every time.

Solution: Use the ${:-} syntax when referring to the parameter,and use it to supply a default value:

 

13.Setting Default Values

Problem: Your script may rely on certain environment variables,either widely used ones or ones specific to your own business. If you want to build a robust shell script,you should make sure that these variables do have a reasonable value.You want to guarantee a reasonable default value.How?

Solution: Use the assignment operator in the shell variable reference the first time you refer to it to assign a value to the variable if it doesn't already have one.

 

14.Using null As a Valid Default Value

Problem: You need to set a default value,but you want to allow an empty string as a valid value.You only want to substitute the default in the case where the value is unset. 

Solution:   

15.Using More Than Just a Constant String for Default

Problem: You need something more than just a constant string as the default value for the variable.

Solution: You can use quire a bit more on the righthand side of these shell variable references.

 

16.Giving an Error Message for Unset Parameters 

Problem: Those shorthands for giving a default value are cool,but maybe you need to force the user to give you a value,otherwise you don't want to proceed.

Solution: Use the ${:?} syntax when referring to the parameter.bash will print an error message and then exit if the parameter is unset or null.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值