pushd和popd的使用(ex37.sh)

本文展示了如何使用bash脚本在不同目录间切换并自动打印当前目录,涉及目录堆栈管理和基本的bash脚本操作。
#!/bin/bash

dir1=/usr/local
dir2=/var/spool

pushd $dir1
# Will do an automatic 'dirs' (list directory stack to stdout).
echo "Now in directory `pwd`." # Uses back-quoted 'pwd'.

# Now, do some stuff in directory 'dir1'.
pushd $dir2
echo "Now in directory `pwd`."

# Now, do some stuff in directory 'dir2'.
echo "The top entry in the DIRSTACK array is $DIRSTACK."
popd
echo "Now back in directory `pwd`."

# Now, do some more stuff in directory 'dir1'.
popd
echo "Now back in original working directory `pwd`."

exit 0

# What happens if you don't 'popd' -- then exit the script?
# Which directory do you end up in? Why?
[root@localhost shell]# ./ex37.sh
/usr/local /home/shell
Now in directory /usr/local.
/var/spool /usr/local /home/shell
Now in directory /var/spool.
The top entry in the DIRSTACK array is /var/spool.
/usr/local /home/shell
Now back in directory /usr/local.
/home/shell
Now back in original working directory /home/shell.

root@fb0699a0389b:/host_home/user/LLM-TPU# cat run.sh #!/bin/bash set -ex # Args parse_args() { while [[ $# -gt 0 ]]; do key="$1" case $key in --model) model="$2" shift 2 ;; --arch) arch="$2" shift 2 ;; *) echo "Invalid option: $key" >&2 exit 1 ;; :) echo "Option -$OPTARG requires an argument." >&2 exit 1 ;; esac done } # Mapping declare -A model_to_demo=( ["chatglm2-6b"]="ChatGLM2" ["chatglm3-6b"]="ChatGLM3" ["llama2-7b"]="Llama2" ["llama3-7b"]="Llama3" ["qwen-7b"]="Qwen" ["qwen1.5-1.8b"]="Qwen1_5" ["qwen2.5-7b"]="Qwen2_5" ["wizardcoder-15b"]="WizardCoder" ["lwm-text-chat"]="LWM" ["internvl2-4b"]="InternVL2" ["minicpmv2_6"]="MiniCPM-V-2_6" ["molmo-7b"]="Molmo" ) # Process Args parse_args "$@" # Check Version compare_date="20240110" if [ $arch == "pcie" ]; then extracted_date=$(cat /proc/bmsophon/driver_version | grep -o 'release date: [0-9]\{8\}' | grep -o '[0-9]\{8\}') elif [ $arch = "soc" ]; then extracted_date_str=$(uname -a | grep -oP 'SMP \K[A-Za-z]+\s[A-Za-z]+\s\d+\s\d+:\d+:\d+\s[A-Za-z]+\s\d+' | sed 's/HKT //') extracted_date=$(date -d "$extracted_date_str" '+%Y%m%d') fi if [ "$extracted_date" -lt "$compare_date" ]; then >&2 echo -e "Your driver is \033[31moutdated\033[0m. Please update your driver." exit 1 else echo "Driver date is $extracted_date, which is up to date. Continuing..." fi # Function to validate model name validate_model() { local model="$1" if [[ ! ${model_to_demo[$model]} ]]; then echo -e "Error: Invalid name $model, the input name must be \033[31m$(printf "%s|" "${!model_to_demo[@]}" | sed 's/|$//')\033[0m" >&2 return 1 fi return 0 } # Check Model Name validate_model "$model" || exit 1 # Compile pushd "./models/${model_to_demo[$model]}" ./run_demo.sh popd root@fb0699a0389b:/host_home/user/LLM-TPU# bash +x run.sh --model /host_home/user/LLM-TPU/models/Qwen2_5 : invalid optionset: - set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...] run.sh: line 3: $'\r': command not found run.sh: line 5: syntax error near unexpected token `$'{\r'' 'un.sh: line 5: `parse_args() {
最新发布
05-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值