问题背景
最近在编译yocto工程,设置环境变量时,突然发现oe-init-build-env无法正常工作了,正常情况下BDIR="build",但莫名奇妙生成了一个x的目录,有点懵。
# normal
dir$ . oe-init-build-env
dir/build$
# abnormal
dir$ . oe-init-build-env
dir/x$
scripts/oe-buildenv-internal
if [ -z "$BDIR" ]; then
if [ -z "$1" ]; then
BDIR="build"
else
BDIR="$1"
...
问题分析
- 怀疑是环境变量,使用env/export/set等命令查看,没有看到特别明显的变量
- 使用其他脚本尝试,问题跟着脚本走 shell.sh脚本内容如下:
#!/bin/bash
echo 0=$0 1=$1 2=$2
echo BASH_SOURCE=$BASH_SOURCE
echo "\$BASH_SOURCE = '$BASH_SOURCE' "
echo "\${BASH_SOURCE[0]} = '${BASH_SOURCE[0]}'"
echo "\${BASH_SOURCE[1]} = '${BASH_SOURCE[1]}'"
echo "\${BASH_SOURCE[2]} = '${BASH_SOURCE[2]}'"
echo $*
echo $@
echo $#
for arg in "$*"; do
echo "****:" $arg
done
echo --------------
for arg in "$@"; do
echo "@@@@:" $arg
done
如下是运行结果,从结果上看,确实有参数,但运行的时候没有输入参数,那什么东西会影响参数呢?
dir$. shell.sh
0=-bash 1=x 2=
BASH_SOURCE=shell.sh
$BASH_SOURCE = 'shell.sh'
${BASH_SOURCE[0]} = 'shell.sh'
${BASH_SOURCE[1]} = ''
${BASH_SOURCE[2]} = ''
x
x
1
****: x
--------------
@@@@: x
在当前shell下进一步确认:
$ echo "$0"
-bash
$ echo "$1"
x
$ echo "$@"
x
$ echo "$*"
x
- 尝试history看看具体做了啥操作,怀疑和set相关,因为set -x是我用来调试shell脚本用的,会不会哪里误操作了?
- 从help和网上资料,学习了set,还真是这个set设置的。set x相当于设置了x参数,类似. shell.sh x一样
$ help set
set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
Set or unset values of shell options and positional parameters.
Change the value of shell attributes and positional parameters, or
display the names and values of shell variables.
Options:
-a Mark variables which are modified or created for export.
-b Notify of job termination immediately.
-e Exit immediately if a command exits with a non-zero status.
-f Disable file name generation (globbing).
-h Remember the location of commands as they are looked up.
-k All assignment arguments are placed in the environment for a
command, not just those that precede the command name.
-m Job control is enabled.
-n Read commands but do not execute them.
-o option-name
Set the variable corresponding to option-name:
allexport same as -a
braceexpand same as -B
emacs use an emacs-style line editing interface
errexit same as -e
errtrace same as -E
functrace same as -T
hashall same as -h
histexpand same as -H
history enable command history
ignoreeof the shell will not exit upon reading EOF
interactive-comments
allow comments to appear in interactive commands
keyword same as -k
monitor same as -m
noclobber same as -C
noexec same as -n
noglob same as -f
nolog currently accepted but ignored
notify same as -b
nounset same as -u
onecmd same as -t
physical same as -P
pipefail the return value of a pipeline is the status of
the last command to exit with a non-zero status,
or zero if no command exited with a non-zero status
posix change the behavior of bash where the default
operation differs from the Posix standard to
match the standard
privileged same as -p
verbose same as -v
vi use a vi-style line editing interface
xtrace same as -x
-p Turned on whenever the real and effective user ids do not match.
Disables processing of the $ENV file and importing of shell
functions. Turning this option off causes the effective uid and
gid to be set to the real uid and gid.
-t Exit after reading and executing one command.
-u Treat unset variables as an error when substituting.
-v Print shell input lines as they are read.
-x Print commands and their arguments as they are executed.
-B the shell will perform brace expansion
-C If set, disallow existing regular files to be overwritten
by redirection of output.
-E If set, the ERR trap is inherited by shell functions.
-H Enable ! style history substitution. This flag is on
by default when the shell is interactive.
-P If set, do not resolve symbolic links when executing commands
such as cd which change the current directory.
-T If set, the DEBUG and RETURN traps are inherited by shell functions.
-- Assign any remaining arguments to the positional parameters.
If there are no remaining arguments, the positional parameters
are unset.
- Assign any remaining arguments to the positional parameters.
The -x and -v options are turned off.
# -- 将任何剩余参数分配给位置参数。如果没有剩余参数,则位置参数未设置。
# - 将任何剩余的参数分配给位置参数。-x 和 -v 选项已关闭。
Using + rather than - causes these flags to be turned off. The
flags can also be used upon invocation of the shell. The current
set of flags may be found in $-. The remaining n ARGs are positional
parameters and are assigned, in order, to $1, $2, .. $n. If no
ARGs are given, all shell variables are printed.
Exit Status:
Returns success unless an invalid option is given.
# add Positional parameters
Positional parameters refer to the input arguments supplied to your scripts during their execution.
These arguments are accessible using variables ranging from $1 to $N, where N represents a numeric value.
位置参数是指在脚本执行期间提供给脚本的输入参数。这些参数可以使用 $1 到 $N 范围内的变量来访问,其中 N 代表数值。
set的相关用法脚本:
#!/bin/bash
echo "$#"
echo "$@"
echo "$*"
# From command line
echo -e "Basename=$0"
echo -e "\$1=$1"
echo -e "\$2=$2"
echo -e "\$3=$3"
# From Set builtin
set first second third
echo -e "\$1=$1"
echo -e "\$2=$2"
echo -e "\$3=$3"
# Store positional parameters with -(hyphen)
set - -f -s -t
echo -e "\$1=$1"
echo -e "\$2=$2"
echo -e "\$3=$3"
set -- -f -s -t
echo -e "\$1=$1"
echo -e "\$2=$2"
echo -e "\$3=$3"
# set --, Unset positional parameter
set --
echo -e "\$1=$1"
echo -e "\$2=$2"
echo -e "\$3=$3"
# "$-" set -
echo "$-"
set -vx
echo "$-"
set -
echo "$-"
小结
- 脚本的参数可以使用set - arg1 arg2来设置,可以使用set --来清除
- set/unset不成对,避免使用set来设置环境变量
参考文档
- https://blog.csdn.net/u012964600/article/details/135402697
- https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#The-Set-Builtin
- https://linuxsimply.com/bash-scripting-tutorial/parameters/positional-parameters/
- https://unix.stackexchange.com/questions/378566/ways-to-set-positional-parameters-in-bash