Unix® Shell Programming读书笔记2

本文深入探讨了Shell编程的关键概念和技术,包括变量处理、条件判断、循环控制、命令执行及调试等。通过具体示例讲解如何运用这些技术解决实际问题,并强调了良好的编程习惯对于编写高效稳定脚本的重要性。

21.Exit Status
The shell variable $? is automatically set by the shell to the exit status of the last command executed. 0---succeeded.1--failed

22.If the user is logged on, we'll print a message to that effect; otherwise we'll say nothing.
if who | grep "^$user " > /dev/null

then

echo "$user is logged on"

fi
the if command executes the pipeline----who | grep "$user".And tests the exit status returned by grep. If the exit status is zero, grep found user in who's output. In that case, the echo command that follows is executed. If the exit status is nonzero, the specified user is not logged on, and the echo command is skipped.

23.test
It's good programming practice to enclose shell variables that are arguments to test inside a pair of double quotes (to allow variable substitution). This ensures that test sees the argument in the case where its value is null.
In the "test",() must has a \ before it.

24.exit
exit n
where n is the exit status that you want returned. If none is specified, the exit status used is that of the last command executed before the exit.Be advised that executing the exit command directly from your terminal will log you off the system because it will have the effect of terminating execution of your login shell.


25.case
? can be used to specify any single character; * can be used to specify zero or more occurrences of any character; and [...] can be used to specify any single character enclosed between the bracket.
| has the effect of a logical OR when used between two patterns.


26.The -x Option for Debugging Shell Programs
You can trace the execution of any program by typing sh -x followed by the name of the program and its arguments. This starts up a new shell to execute the indicated program with the -x option enabled. In this mode, commands are printed at the terminal as they are executed, preceded by a plus sign.

27.The Null Command :
The format of this command is simply ----- :

28.&& and || and if-else
If condition A is succeeded,run the B, the if-else can be replaced with &&,otherwise with ||

29.for
f the file filelist contains a list of the files that you want to run through run, you can type
files=$(cat filelist)
for file in $files

do

run $file

done

go through all the arguments passed on
for arg in "$@"

do

echo $arg

done
It's equal to the following structure:
for arg

do

echo $arg

done

30.while and until
The while loop is often used in conjunction with the shift command to process a variable number of arguments typed on the command line.
while [ "$#" -ne 0 ]

do

echo "$1"

shift

done
The while command continues execution as long as the command listed after the while returns a zero exit status. The until command is similar to the while, only it continues execution as long as the command that follows the until returns a nonzero exit status.The until command is useful for writing programs that wait for a particular event to occur.
To just exit from the loop (and not from the program), you can use the break command.
If the break command is used in the form: break n ,the n innermost loops are immediately exited.
The continue command is similar to break, only it doesn't cause the loop to be exited, merely the remaining commands in the loop to be skipped. Execution of the loop then continues as normal. Like the break, an optional number can follow the continue, so continue n causes the commands in the innermost n loops to be skipped; but execution of the loops then continues as normal.
An entire loop can be sent to the background for execution simply by placing an ampersand after the done: done & .
I/O Redirection on a Loop: done > loopout:eg.echo's output is redirected to the terminal while the rest goes to the file output.
for file

do

echo "Processing file $file" > /dev/tty

...

done > output
A command's output can be piped into a loop, and the entire output from a loop can be piped into another command in the expected manner:done | wc -l




31.Death Loop
The Unix command true serves no purpose but to return an exit status of zero. The command false also does nothing but return a nonzero exit status. If you write

while true

do

...

done <=>

while :
do

...

done <=>
until false

do

...

done

32.getopts
The getopts command is designed to be executed inside a loop. Each time through the loop, getopts examines the next command line argument and determines whether it is a valid option.

#

# Wait until a specified user logs on -- version 3

#

# Set up default values

mailopt=FALSE

interval=60

# process command options
# If getopts doesn't find an argument after an option that requires one, it stores a question mark inside the specified # variable and writes an error message to standard error. Otherwise, it stores the actual argument inside a special # variable called OPTARG.

while getopts mt: option

do

case "$option"

in

m) mailopt=TRUE;;

t) interval=$OPTARG;;

\?) echo "Usage: mon [-m] [-t n] user"

echo " -m means to be informed by mail"

echo " -t means check every n secs."

exit 1;;

esac

done

# Make sure a user name was specified
# Another special variable called OPTIND is used by the command. This variable is initially set to one and is updated each # time getopts returns to reflect the number of the next command-line argument to be processed.

if [ "$OPTIND" -gt "$#" ]

then

echo "Missing user name!"

exit 2

fi

shiftcount=$((OPTIND - 1))

shift $shiftcount

user=$1

#

# Check for user logging on

#

until who | grep "^$user " > /dev/null

do

sleep $interval

done

#

# When we reach this point, the user has logged on

#

if [ "$mailopt" = FALSE]

then

echo "$user has logged on"

else

runner=$(who am i | cut -c1-8)

echo "$user has logged on" | mail $runner

fi
explanation:
When the line mon -m -t 600 ann & is executed, the following occurs inside the while loop in mon: getopts is executed, and it stores the character m inside the variable option, sets OPTIND to two, and returns a zero exit status. The case command is then executed to determine what was stored inside option. A match on the character m indicates that the "send mail" option was selected, so mailopt is set to TRUE. (Note that the ? inside the case is quoted. This is to remove its special meaning as a pattern-matching character from the shell.)
The second time getopts is executed, getopts stores the character t inside option, stores the next command-line argument (600) inside OPTARG, sets OPTIND to three, and returns a zero exit status. The case command then matches the character t stored inside option. The code associated with that case copies the value of 600 that was stored in OPTARG into the variable interval.
The third time getopts is executed, getopts returns a nonzero exit status, indicating the end of options. The program then checks the value of OPTIND against $# to make sure that the username was typed on the command line. If OPTIND is greater than $#, then no more arguments remain on the command line and the user forgot the username argument. Otherwise, the shift command is executed to move the username argument into $1. The actual number of places to shift is one less than the value of OPTIND.


33.Special echo Escape Characters
The echo command always automatically displays a terminating newline character after the last argument.
This can be suppressed if the last two characters given to echo are the special escape characters \c. This tells echo to leave the cursor right where it is after displaying the last argument and not to go to the next line.

34.Temporary files

When writing shell programs to be run by more than one person, make your temporary files unique. One way is to create the temporary file in the user's home directory, for example. Another way is to choose a temporary filename that will be unique for that particular process. To do this, you can use the special $$ shell variable, which contains the process id number (PID) of the current process.

35.The Exit Status from read
read always returns an exit status of zero unless an end of file condition is detected on the input.

lineno=1

cat $* |

while read -r line

do

echo "$lineno: $line"

lineno=$((lineno + 1))

done

The variable lineno--the line number count--is initially set to 1. Then the arguments typed to number are given to cat to be collectively written to standard output. If no arguments are supplied, $* will be null, and cat will be passed no arguments. This will cause it to read from standard input.The output from cat is piped into the while loop. For each line read by read, the line is echoed at the terminal, preceded by the value of lineno, whose value is then incremented by one.
Use the -r option to read to prevent it from interpreting the special character----backslash.

36.printf
printf "format" arg1 arg2 ...
format is : %[flags][width][.precision]type flags: -, justifies the value being printed(- means left aligned,right aligned default)
+, precede integers with a + or - sign
#, precede octal integers with 0 and hexadecimal integers with 0x or 0X for %#x or %#X,

Space character, precede positive integers with a space and negative integers with a - width: a positive number that specifies the minimum field width for printing an argument. The argument is right justified within this field unless the - flag is used.
.precision:a positive number that specifies a minimum number of digits to be displayed for %d, %u, %o, %x, and %X. This results in zero padding on the left of the value.
If a * is used in place of a number for width or precision, the argument preceding the value to be printed must be a number and will be used as the width or precision, respectively. If a * is used in place of both, two integer arguments must precede the value being printed and are used for the width and precision:

$ printf "%*s%*.*s\n" 12 "test one" 10 2 "test two"

test one te
12 is used as the width for the first string, 10 as the width for the second string, and 2 as the precision for the second string.

37. Subshell
There is a way to make the value of a variable known to a subshell, and that's by exporting it with the export command. The format of this command is simply:export variables.
1)Any variable that is not exported is a local variable whose existence will not be known to subshells.
2)Exported variables and their values are copied into a subshell's environment, where they may be accessed and changed. However, such changes have no effect on the variables in the parent shell.There is no way to change the value of a variable in a parent shell from within a subshell.
3)Exported variables retain this characteristic not only for directly spawned subshells, but also for subshells spawned by those subshells (and so on down the line).
4)A variable can be exported any time before or after it is assigned a value.

38.PS1 and PS2
PS1:The characters that the shell displays as your command prompt are stored in it.
PS2:The characters that used in the concatenation of two lines

39.$PATH
To have the current directory searched first, you put the period at the start of the path:.:/bin:/usr/bin
(can cause so-called Trojan horse problem)
Using a environment variable can be multi-used by many users.

40. There is no way to change the current directory of a parent shell from a subshell.

内容概要:本文设计了一种基于PLC的全自动洗衣机控制系统内容概要:本文设计了一种,采用三菱FX基于PLC的全自动洗衣机控制系统,采用3U-32MT型PLC作为三菱FX3U核心控制器,替代传统继-32MT电器控制方式,提升了型PLC作为系统的稳定性与自动化核心控制器,替代水平。系统具备传统继电器控制方式高/低水,实现洗衣机工作位选择、柔和过程的自动化控制/标准洗衣模式切换。系统具备高、暂停加衣、低水位选择、手动脱水及和柔和、标准两种蜂鸣提示等功能洗衣模式,支持,通过GX Works2软件编写梯形图程序,实现进洗衣过程中暂停添加水、洗涤、排水衣物,并增加了手动脱水功能和、脱水等工序蜂鸣器提示的自动循环控制功能,提升了使用的,并引入MCGS组便捷性与灵活性态软件实现人机交互界面监控。控制系统通过GX。硬件设计包括 Works2软件进行主电路、PLC接梯形图编程线与关键元,完成了启动、进水器件选型,软件、正反转洗涤部分完成I/O分配、排水、脱、逻辑流程规划水等工序的逻辑及各功能模块梯设计,并实现了大形图编程。循环与小循环的嵌; 适合人群:自动化套控制流程。此外、电气工程及相关,还利用MCGS组态软件构建专业本科学生,具备PL了人机交互C基础知识和梯界面,实现对洗衣机形图编程能力的运行状态的监控与操作。整体设计涵盖了初级工程技术人员。硬件选型、; 使用场景及目标:I/O分配、电路接线、程序逻辑设计及组①掌握PLC在态监控等多个方面家电自动化控制中的应用方法;②学习,体现了PLC在工业自动化控制中的高效全自动洗衣机控制系统的性与可靠性。;软硬件设计流程 适合人群:电气;③实践工程、自动化及相关MCGS组态软件与PLC的专业的本科生、初级通信与联调工程技术人员以及从事;④完成PLC控制系统开发毕业设计或工业的学习者;具备控制类项目开发参考一定PLC基础知识。; 阅读和梯形图建议:建议结合三菱编程能力的人员GX Works2仿真更为适宜。; 使用场景及目标:①应用于环境与MCGS组态平台进行程序高校毕业设计或调试与运行验证课程项目,帮助学生掌握PLC控制系统的设计,重点关注I/O分配逻辑、梯形图与实现方法;②为工业自动化领域互锁机制及循环控制结构的设计中类似家电控制系统的开发提供参考方案;③思路,深入理解PL通过实际案例理解C在实际工程项目PLC在电机中的应用全过程。控制、时间循环、互锁保护、手动干预等方面的应用逻辑。; 阅读建议:建议结合三菱GX Works2编程软件和MCGS组态软件同步实践,重点理解梯形图程序中各环节的时序逻辑与互锁机制,关注I/O分配与硬件接线的对应关系,并尝试在仿真环境中调试程序以加深对全自动洗衣机控制流程的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值