【发布时间】:2020-10-09 16:35:52
【问题描述】:
我正在尝试制作自定义命令提示符。例如,如果我输入
. filename.sh 1 hello 2 hi 3 a 0 b
自定义提示应如下所示:
[b][hello][hi][a]$
数字 1 代表第二个位置,数字 2 代表第三个位置等。但是当我运行它时,不知何故只显示第一部分,如下所示:
[][hello][][]$
而且我无法覆盖它。例如,当我输入
. filename.sh 2 hi
它应该是[][hello][hi][]$,但它却变成了[][][hi][]$。我该如何解决?
#!/bin/bash
#$1 [$2] $3 [$4] $5 [$6] $7 [$8]
PS1="[][][][]$"
for i in "$*"
do
#$1
if [ $1 -eq 0 ]
then
PS1="[$2][][][]$"
elif [ $1 -eq 1 ]
then
PS1="[][$2][][]$"
elif [ $1 -eq 2 ]
then
PS1="[][][$2][]$"
elif [ $1 -eq 3 ]
then
PS1="[][][][$2]$"
#$3
elif [ $3 -eq 0 ]
then
PS1="[$4][][][]$"
elif [ $3 -eq 1 ]
then
PS1="[][$4][][]$"
elif [ $3 -eq 2 ]
then
PS1="[][][$4][]$"
elif [ $3 -eq 3 ]
then
PS1="[][][][$4]$"
#5
elif [ $5 -eq 0 ]
then
PS1="[$6][][][]$"
elif [ $5 -eq 1 ]
then
PS1="[][$6][][]$"
elif [ $5 -eq 2 ]
then
PS1="[][][$6][]$"
elif [ $5 -eq 3 ]
then
PS1="[][][][$6]$"
#7
elif [ $7 -eq 0 ]
then
PS1="[$8][][][]$"
elif [ $7 -eq 1 ]
then
PS1="[][$8][][]$"
elif [ $7 -eq 2 ]
then
PS1="[][][$8][]$"
elif [ $7 -eq 3 ]
then
PS1="[][][][$8]$"
fi
done
【问题讨论】:
标签: linux shell for-loop arguments command-line-arguments