新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
快速上手Linuxset命令
set命令作用主要是顯示系統(tǒng)中已經(jīng)存在的shell變量,以及設(shè)置shell變量的新變量值。使用set更改shell特性時,符號”+”和”-“的作用分別是打開和關(guān)閉指定的模式。set命令不能夠定義新的shell變量。如果要定義新的變量,可以使用declare命令以變量名=值的格式進(jìn)行定義即可。

set命令的常用參數(shù)及作用
-
set
不帶參數(shù)的set命令用來顯示環(huán)境變量。
root@3500f62fe5ae:/workspace# set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="3" [2]="48" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")
BASH_VERSION='4.3.48(1)-release'
COLUMNS=236
CUDA_HOME=/usr/local/cuda
CUDA_PKG_VERSION=10-0=10.0.130-1
CUDA_VERSION=10.0.130
CUDNN_VERSION=7.6.0.64
...
-
set -e
-e參數(shù)表示只要shell腳本中發(fā)生錯誤,即命令返回值不等于0,則停止執(zhí)行并退出shell。set -e在shell腳本中經(jīng)常使用。默認(rèn)情況下,shell腳本碰到錯誤會報錯,但會繼續(xù)執(zhí)行后面的命令。
test.sh腳本內(nèi)容如下:
#!/usr/bin/env bash
set -e
hello
echo "Hello set"
執(zhí)行結(jié)果如下:
root@3500f62fe5ae:/workspace# sh test.sh
test.sh: 4: test.sh: hello: not found
注:set +e表示關(guān)閉-e選項,set -e表示重新打開-e選項。
-
set -u
-u參數(shù)表示shell腳本執(zhí)行時如果遇到不存在的變量會報錯并停止執(zhí)行。默認(rèn)不加-u參數(shù)的情況下,shell腳本遇到不存在的變量不會報錯,會繼續(xù)執(zhí)行。
test.sh腳本內(nèi)容如下:
#!/usr/bin/env bash
echo $test
set -u
echo $hello
執(zhí)行結(jié)果如下:
root@3500f62fe5ae:/workspace# sh test.sh
test.sh: 5: test.sh: hello: parameter not set
當(dāng)前文章:快速上手Linuxset命令
本文鏈接:http://www.dlmjj.cn/article/cocdooe.html


咨詢
建站咨詢
