Bash special variable:$#, $$, $@, $*, $0, $?

本文详细介绍了Shell脚本中六个特殊变量的功能与用法:$#用于获取传递给脚本的参数数量;$$返回当前shell进程号;$@将所有参数作为独立元素处理;$*将所有参数作为一个整体;$0给出当前脚本文件名;$?表示前一个命令的退出状态。
special variable:$#, $$, $@, $*, $0, $?

[b]1, $#[/b]
[b]The number of arguments supplied to a script.[/b]
eg:
if [ "$#" -eq 0 ]; then
echo "you did not pass any parameter"
fi

echo "Total Number of Parameters : $#"
Total Number of Parameters : 2

[b]2, $$[/b]
The process number of the current shell. For shell scripts, this is the process ID under which they are executing.

[b]3, $@[/b]
the "$@" special parameter takes the entire list and separates it into separate arguments.
All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.

[b]4, $*[/b]
the "$*" special parameter takes the entire list as one argument with spaces between.
All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.

[b]5, $0[/b]
The filename of the current script.

[b]6, $?[/b]
[b]represents the exit status of the previous command.[/b]
[color=red]0 if they were successful, and 1 if they were unsuccessful.[/color]
eg:
touch test.ksh
write in test.ksh as below:
-----------------------
#create a stored procedure, name do_feed_processing.
do_feed_processing() {
....
return 0
}
#execute do_feed_processing
do_feed_processing
#print execution result
echo $?
-----------------------
$ sh test.ksh
$ 0
#!/bin/bash # 2)拆分ipf和特定机型的sdk,流程类似 # 先克隆一个本地仓库tmp1供拆,并创建对应的远端仓库,该仓库信息需位于txt的末尾 # 然后基于ipf.txt/sdk.txt拆解子仓库:创建远端仓库、克隆本地仓库tmp2、拆解出子仓库并提交至远端、拆解tmp1仓库的对应部分 # 最后提交tmp1仓库远端,该仓库拆解完毕 set -uo pipefail # Remove -e so script does not exit on error INPUT_FILE="sdk_repo_split.txt" GERRIT_HOST="gerrittest.tp-link.com" GERRIT_USER="longyufei" PORT=29418 IP="10.176.69.131:29418" sed -i 's/\r$//' "$INPUT_FILE" # Read the whole file into a variable filedata=$(<"$INPUT_FILE") # Split into blocks on blank lines IFS='' blocks=() block="" while IFS= read -r line || [[ -n "$line" ]]; do if [[ -z "$line" ]]; then if [[ -n "$block" ]]; then blocks+=("$block") block="" fi else if [[ -z "$block" ]]; then block="$line" else block="$block"$'\n'"$line" fi fi done <<< "$filedata" # Add last block if missing a trailing blank line if [[ -n "$block" ]]; then blocks+=("$block") fi # the new big repo msg <-> tmp1 OLD_REPO=$(sed -n '$p' <<< "${blocks[-1]}") SDK_REPO=$(sed -n '1p' <<< "${blocks[-1]}") SDK_DESC=$(sed -n '2,5p' <<< "${blocks[-1]}") TMP_DIR_1="tmp1" # the new big remote repo echo -e "repo_name: $OLD_REPO" output=$(ssh -p "$PORT" "$GERRIT_USER@$GERRIT_HOST" \ "gerrit create-project \"$SDK_REPO\" --description \"$SDK_DESC\" --submit-type MERGE_IF_NECESSARY" 2>&1) # the new big local repo TMP_DIR_1="tmp1" rm -rf "$TMP_DIR_1" git clone --no-local "$OLD_REPO" "$TMP_DIR_1" >/dev/null # the sub repos splitted echo "==== start delete sub-repos ====" if [[ ${#blocks[@]} -gt 0 ]]; then all_but_last=("${blocks[@]:0:${#blocks[@]}-1}") for block in "${all_but_last[@]}"; do repo=$(sed -n '1p' <<< "$block") desc=$(sed -n '2,5p' <<< "$block") localpath=$(sed -n '6p' <<< "$block") OLD_PATH=$(echo "$localpath" | sed "s#^${OLD_REPO}/##") if [[ -n "$repo" && -n "$desc" ]]; then # build sub repo with desc echo "Creating repository: $repo" echo "==== DESCRIPTION START ====" echo -e "$desc" echo "==== DESCRIPTION END ====" # Temporarily disable exit on error for this command set +e output=$(ssh -p "$PORT" "$GERRIT_USER@$GERRIT_HOST" \ "gerrit create-project \"$repo\" --description \"$desc\" --submit-type MERGE_IF_NECESSARY" 2>&1) status=$? set -e if [[ $status -ne 0 ]]; then if [[ "$output" == *"already exists"* ]]; then echo "⏭️ Repo already exists, skipped: $repo" else echo "⚠️ Failed to create: $repo" echo "$output" fi else echo "✅ Created: $repo" # split the local repo and pull fi echo "===== SPLIT START =====" # clone the local repo and split to the target sub-repo TMP_DIR_2="tmp2" rm -rf "$TMP_DIR_2" echo "===== SPLIT OLD_REPO:$OLD_REPO OLD_PATH:$OLD_PATH =====" git clone --no-local "$OLD_REPO" "$TMP_DIR_2" >/dev/null cd "$TMP_DIR_2" git filter-repo --subdirectory-filter "$OLD_PATH" echo "==== SPLIT END ====" # modify the auther-name and auther-email git filter-repo --force --commit-callback ' commit.author_name=b"xxx" commit.author_email=b"xxx@example.com" ' # push the target sub-repo to origin git remote add origin ssh://"$GERRIT_USER@$IP/$repo" git remote set-url origin ssh://"$GERRIT_USER@$IP/$repo" git push origin --all cd ../ # delete the sub-repo from the big repo echo "==== REPO delete $repo start ====" cd "$TMP_DIR_1" git filter-repo --path "$OLD_PATH" --invert-paths echo "==== REPO delete $repo end ====" cd ../ fi done else echo "没有找到任何文本块" fi # push the new big repo to origin echo "==== NEW REPO PUSH START ====" cd "$TMP_DIR_1" # modify the auther-name and auther-email # git filter-repo --force --commit-callback ' # commit.author_name=b"xxx" # commit.author_email=b"xxx@example.com" # ' git remote add origin ssh://"$GERRIT_USER@$IP/$SDK_REPO" 2>/dev/null || \ git remote set-url origin ssh://"$GERRIT_USER@$IP/$SDK_REPO" git push origin --all echo "==== NEW REPO PUSH END ====" 需要在上述脚本中加个逻辑,如果传进去的需要识别拆分的目录是target/linux/linux-5.4.271,那么删除在拆除旧仓库的每一个分支时,都需要删除旧仓库中对应目录下的target/linux/linux-5.* 不管最终版本号是多少
12-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值