当前位置:网站首页>Shell unknown rollup 1
Shell unknown rollup 1
2022-06-28 18:47:00 【Dreamer DBA】
1.1 Count the number of parameters
problem : You want to know how many parameters are used when calling the script 、
Solution : Use shell Built in variables $#
#!/usr/bin/env bash
# instance file : check_arg_count
#
# check out the numbers of the correct parameter
# use the grammer as below: if [ $# -lt 3 ]
if (( $# < 3))
then
printf "%b" "Error. Not enough arguments.\n" >&2
printf "%b" "usage: myscript file1 op file2\n" >&2
elif(( $# > 3))
then
printf "%b" "Error.Too many arguments.\n" >&2
printf "%b" "usage: myscript file1 op file2\n" >&2
else
printf "%b" "Argument count correct.Proceeding...\n"
fi1.2 Send output and error messages to the same file
problem : Use redirection , We can save output or error messages to a separate file , But how to send both to the same file ?
Solution : use shell The syntax redirects the standard error message to the same place as the standard output .
The preferred
both >& outfile
perhaps :
both &> outfile
both The representative is ready to STDERR and STDOUT The program that generates the output .
Discuss
&> and >& It's just take STDOUT and STDERR Send to the same place .
both 2> outfile 1>&2
1 For standard output
2 Represents standard error
1.3 Use... Correctly logger
problem : You want to use logger Tools enable scripts to send syslog news , But by default ,logger Not providing enough useful information .
Solution :
logger -t "${0##*/}[$$]" 'Your message here'
If not used logger Of -t Options , Even if it's not a fatal mistake By mistake , At least you have to accept the warning .t representative “ label ”(tag), Follow the instructions in the man page said , It will “ Use the specified label to mark each line of recorded content ”. In other words, say , If not -t Options , It's hard for you to know where these log messages come from .
${0##*/}[$$] Labels may look like garbled code , But view syslog logging You will often see it . The label is just The basic name of the script and In square brackets The process of ID($$) nothing more . Compare using and not using -t Option logger.
1.4 typeset
typeset Declare variables and assign attributes , Equate to declare
1.5 Find out if the command ran successfully
problem : You need to know if the command runs successfully
Solution : Write a command or shell Script programmers follow established conventions , that shell Variable $? Will be set to not... When the command fails 0 value .
$ somecommand
# it works...
$ echo $?
0
$ badcommand
# it fails...
$ echo $?
1
$Discuss : shell Variable $? The exit status of the command is saved in , Its value range is 0·255
Writing shell Script time , Good practice is : If everything goes well , When the script exits return 0; If there is an error during operation , Then return to non 0 value . We recommend using only 0~127 As return value , because shell use 128+N Represents the received signal N“ kill die ”. in addition , If the value used is greater than 255 Or less than 0, Value wrapping will occur . Sure use exit sentence ( Such as exit 1 or exit 0) Return to exit state . But to Be careful , There is only one chance to read the command exit status
however $? The real use of this variable is In terms of scripting . If you watch the command run on the screen , You can usually see this command If there is something wrong . But in the script , The command may run unattended .
bash One of the characteristics of , The scripting language is identical to the commands you type at the terminal window prompt identical . When writing scripts , This makes checking grammar and logic much easier .
Exit status is mostly used for scripts and if In the sentence , Depending on the success of the command being executed Different ways of handling . The following example is simple , But we will discuss this later topic of conversation .
somecommand
...
if (( $? )) ; then echo failed ; else echo OK; fi
(( )) Evaluate an arithmetic expression
We do not recommend using negative numbers .shell Negative numbers are acceptable , No errors reported , But it didn't Not as you wish .
$ bash -c 'exit -2' ; echo $?
254
$ bash -c 'exit -200' ; echo $?
56
边栏推荐
猜你喜欢

618 activity season - the arrival of special discounts for hundreds of low code platforms

Shanghai Pudong Development Bank Software Test interview real question

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?

Seata implementation of sharing JDBC distributed transaction

内存泄露

模块化操作

MindSpore系列一加载图像分类数据集

POI Excel转换工具

电子商务盛行,怎么提高商店转换率?

解析机器人主持教学的实践发展
随机推荐
获取当前日期0点及23点59时间戳
【Unity3D】相机跟随
Shell 未知汇总1
C# 41. Int to string
进阶高级-业务事务设计 开发入门
基于管线的混合渲染
Win 10创建一个gin框架的项目
ANR Application Not Responding
What are the design requirements for PCB layout and wiring?
Steam education to break the barriers between disciplines
深入解析kubernetes中的选举机制
Graphic system - 1 Layout loading
声网 VQA:将实时互动中未知的视频画质用户主观体验变可知
async-validator.js数据校验器
Detailed explanation of select in golang (forward)
io模型初探
图形系统——2.view绘制
Lumiprobe 蛋白质标记研究方案
使用Karmada实现Helm应用的跨集群部署
Sharing-JDBC分布式事务之Seata实现