File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 347347利用这一点,可以在脚本中对命令执行结果进行判断。
348348
349349``` bash
350- cd $some_directory
350+ cd /path/to/somewhere
351351if [ " $? " = " 0" ]; then
352352 rm *
353353else
@@ -356,12 +356,12 @@ else
356356fi
357357```
358358
359- 上面例子中,` cd $some_directory ` 这个命令如果执行成功(返回值等于` 0 ` ),就删除该目录里面的文件,否则退出脚本,整个脚本的返回值变为` 1 ` ,表示执行失败。
359+ 上面例子中,` cd /path/to/somewhere ` 这个命令如果执行成功(返回值等于` 0 ` ),就删除该目录里面的文件,否则退出脚本,整个脚本的返回值变为` 1 ` ,表示执行失败。
360360
361361由于` if ` 可以直接判断命令的执行结果,执行相应的操作,上面的脚本可以改写成下面的样子。
362362
363363``` bash
364- if cd $some_directory ; then
364+ if cd /path/to/somewhere ; then
365365 rm *
366366else
367367 echo " Could not change directory! Aborting." 1>&2
373373
374374``` bash
375375# 第一步执行成功,才会执行第二步
376- cd $some_directory && rm *
376+ cd /path/to/somewhere && rm *
377377
378378# 第一步执行失败,才会执行第二步
379- cd $some_directory || exit 1
379+ cd /path/to/somewhere || exit 1
380380```
381381
382382## source 命令
You can’t perform that action at this time.
0 commit comments