当前位置:网站首页>Chapter 19 Tips and Traps: Common Goofs for Novices
Chapter 19 Tips and Traps: Common Goofs for Novices
2022-08-01 22:59:00 【梦想家DBA】
1.1 Forgetting to Set Execute Permissions
First, you could invoke bash and give it the name of the script as a parameter:
Or second (and better still), you could set the execute permission on the script so that you can run it directly:
[[email protected] test]$ ./fun_calc.sh
-bash: ./fun_calc.sh: Permission denied
[[email protected] test]$ bash fun_calc.sh
[[email protected] test]$ chmod a+x fun_calc.sh
[[email protected] test]$ ./fun_calc.sh
[[email protected] test]$1.2 Fixing "No such file or directory" Errors
[[email protected] Day0801]$ cat busted
#!/bin/bash -
echo "Hello World!"
[[email protected] Day0801]$ # This works
[[email protected] Day0801]$ ./busted
Hello World!
[[email protected] Day0801]$ # But if the file gets DOS line endings,we get:
[[email protected] Day0801]$ ./busted1
/bin/bash: will: No such file or directory
Hello World!
[[email protected] Day0801]$
1.3 Forgetting That the Current Directory Is Not in the $PATH
Either add the current directory to the $PATH variable, which we do not recommend, or reference the script via the current directory with a leading ./ before the script name, as in: $ ./busted
[[email protected] Day0801]$ busted
bash: busted: command not found
[[email protected] Day0801]$ ./busted
Hello World!
[[email protected] Day0801]$1.4 Naming Your Script Test
[[email protected] Day0801]$ type test
test is a shell builtin
[[email protected] Day0801]$ 1.5 Expecting to Change Exported Variables
[[email protected]xwellDBA Day0801]$ ./first.sh
VAL=5
in second
initially VAL=5
changed so VAL=12
back in first
VAL=5
[[email protected] Day0801]$ cat first.sh
#
# a simple example of a common mistake
#
# set the value:
export VAL=5
printf "VAL=%d\n" $VAL
#invoke our other script:
./second.sh
#
# now see what changed (hint:nothing!)
printf "%b" "back in first\n"
printf "VAL=%d\n" $VAL
[[email protected] Day0801]$ cat second.sh
#!/bin/bash
printf "%b" "in second\n"
printf "initially VAL=%d\n" $VAL
VAL=12
printf "changed so VAL=%d\n" $VAL
[[email protected] Day0801]$and the second script has to echo the final value (and only the final value) to STDOUT (it could redirect its other messages to STDERR):
[[email protected] Day0801]$ ./first.sh
VAL=5
in second
initially VAL=5
changed so VAL=12
back in first
VAL=5
[[email protected] Day0801]$ cat first.sh
#
# a simple example of a common mistake
#
# set the value:
export VAL=5
printf "VAL=%d\n" $VAL
#invoke our other script:
./second.sh
#
# now see what changed (hint:nothing!)
printf "%b" "back in first\n"
printf "VAL=%d\n" $VAL
[[email protected] Day0801]$ cat second.sh
#!/bin/bash
printf "%b" "in second\n" >&2
printf "initially VAL=%d\n" $VAL >&2
VAL=12
printf "changed so VAL=%d\n" $VAL >&2
[[email protected] Day0801]$ 1.6 Forgetting Quotes Leads to "command not found" on Assignments
You need quotes around the righthand side of the assignment to $ALLOPT. What is written above as: ALLOPT=$OPT1 $OPT2 really should be: ALLOPT="$OPT1 $OPT2"
[[email protected] Day0801]$ cat goof1.sh
#!/bin/bash -
# common goof:
# X=$Y $Z
# isn't the same as
# X="$Y $Z"
#
OPT1=-l
OPT2=-h
ALLOPT=$OPT1 $OPT2
ls $ALLOPT .
[[email protected] Day0801]$ chmod a+x goof1.sh
[[email protected] Day0801]$ ./goof1.sh
./goof1.sh: line 9: -h: command not found
busted busted1 dash.sh default_date.sh first.sh goof1.sh load_mp3.sh oodiff.sh second.sh
[[email protected] Day0801]$ cat goof1.sh
#!/bin/bash -
# common goof:
# X=$Y $Z
# isn't the same as
# X="$Y $Z"
#
OPT1=-l
OPT2=-h
ALLOPT="$OPT1 $OPT2"
ls $ALLOPT .
[[email protected] Day0801]$ ./goof1.sh
total 36K
-rwxrwxr-x 1 maxwell maxwell 34 Aug 1 19:33 busted
-rwxrwxr-x 1 maxwell maxwell 58 Aug 1 19:39 busted1
-rw-rw-r-- 1 maxwell maxwell 595 Aug 1 13:27 dash.sh
-rw-rw-r-- 1 maxwell maxwell 915 Aug 1 08:11 default_date.sh
-rwxrwxr-x 1 maxwell maxwell 228 Aug 1 19:49 first.sh
-rwxrwxr-x 1 maxwell maxwell 123 Aug 1 19:59 goof1.sh
-rw-rw-r-- 1 maxwell maxwell 1.4K Aug 1 13:49 load_mp3.sh
-rw-rw-r-- 1 maxwell maxwell 1.3K Aug 1 13:58 oodiff.sh
-rwxrwxr-x 1 maxwell maxwell 145 Aug 1 19:54 second.sh
[[email protected] Day0801]$
1.10 Deleting Files Using an Empty Variable
Never do:
rm -rf $files_to_delete
Never, ever, ever do:
rm -rf /$files_to_delete
Use this instead:
[ "$files_to_delete" ] && rm -rf $files_to_delete1.11 Seeing Odd Behavior from printf
[[email protected] Day0801]$ cat oddscript
#!/bin/bash -
badnode=6
printf "good nodes: %d\n" $goodnode
printf "bad nodes: %d\n" $badnode
printf "miss nodes: %d\n" $missnode
printf "GOOD=%d BAD=%d MISS=%d\n" $goodnode $badnode $missnode
[[email protected] Day0801]$ bash oddscript
good nodes: 0
bad nodes: 6
miss nodes: 0
GOOD=6 BAD=0 MISS=0
[[email protected] Day0801]$ 1.13 Debugging Scripts
Add set -x to the top of the script when you run it. Or use set -x to turn on xtrace before a troublesome spot and set +x to turn it off after.
[[email protected] Day0801]$ cat buggy
#!/usr/bin/env bash
# cookbook filename: buggy
#
set -x
result=$1
[ $result = 1 ] \
&& { echo "Result is 1; excellent." ; exit 0; } \
|| { echo "Uh-oh, ummm, RUN AWAY! " ; exit 120; }Now we invoke this script, but first we set and export the value of the PS4 prompt. bash will print out the value of PS4 before each command that it displays during an execution trace (i.e., after a set -x ):
[[email protected] Day0801]$ cat buggy
#!/usr/bin/env bash
# cookbook filename: buggy
#
set -x
result=$1
[ $result = 1 ] \
&& { echo "Result is 1; excellent." ; exit 0; } \
|| { echo "Uh-oh, ummm, RUN AWAY! " ; exit 120; }
[[email protected] Day0801]$ export PS4='+trace $LINENO:'
[[email protected] Day0801]$ echo $PS4
+trace $LINENO:
[[email protected] Day0801]$ chmod a+x buggy
[[email protected] Day0801]$ ./buggy
+trace 6:result=
+trace 8:'[' = 1 ']'
./buggy: line 8: [: =: unary operator expected
+trace 10:echo 'Uh-oh, ummm, RUN AWAY! '
Uh-oh, ummm, RUN AWAY!
+trace 10:exit 120
[[email protected] Day0801]$ ./buggy 1
+trace 6:result=1
+trace 8:'[' 1 = 1 ']'
+trace 9:echo 'Result is 1; excellent.'
Result is 1; excellent.
+trace 9:exit 0
[[email protected] Day0801]$ ./buggy 2
+trace 6:result=2
+trace 8:'[' 2 = 1 ']'
+trace 10:echo 'Uh-oh, ummm, RUN AWAY! '
Uh-oh, ummm, RUN AWAY!
+trace 10:exit 120
[[email protected] Day0801]$边栏推荐
- 2022 edition of MySQL tutorial, top collection good, take your time
- When solving yolov5 training: "AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train"
- xctf attack and defense world web master advanced area webshell
- 13、学习MySQL 分组
- 2022年最新河北建筑八大员(机械员)模拟考试题库及答案
- 从0到100:招生报名小程序开发笔记
- 图论——强连通分量缩点+拓扑排序
- JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法
- B. Difference Array--Codeforces Round #808 (Div. 1)
- Graph Theory - Strongly Connected Component Condensation + Topological Sort
猜你喜欢

华为无线设备配置全局双链路冷备份(AC全局配置方式)

2022年最新河北建筑八大员(机械员)模拟考试题库及答案
![[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)](/img/26/4c3080f1b21efb9401d8c3a55bc15d.png)
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)

【开源】Sentinel高性能高可用集群限流解决方案

Postman 批量测试接口详细教程

Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (4) Opening Report

美赞臣EDI 940仓库装运订单详解

解决yolov5训练时出现:“AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train ”

Quarantine and downgrade

PDF转Word有那么难吗?做一个文件转换器,都解决了
随机推荐
图论——强连通分量缩点+拓扑排序
华为无线设备配置全局双链路冷备份(AC全局配置方式)
Deep Learning Course2 Week 2 Optimization Algorithms Exercises
xctf攻防世界 Web高手进阶区 web2
PHP算法之有效的括号
用virtualenv和Virtualenvwrapper虚拟环境管理工具创建虚拟环境
将vim与系统剪贴板的交互使用
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)
1391D. 505 状压dp
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D 题解
Use Jenkins for continuous integration, this knowledge point must be mastered
xctf attack and defense world web master advanced area web2
还在纠结报表工具的选型么?来看看这个
CF1703G Good Key, Bad Key
联邦学习入门
小程序中的多表联合查询
xctf攻防世界 Web高手进阶区 webshell
Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
力扣第 304 场周赛复盘
[机缘参悟-58]:《素书》-5-奉行仁义[遵义章第五]