Bash 将可选字符从低级改为高级,从高级改为低级-Unix shell脚本
如何转换传递给脚本的字符串的可选字符,如果该字符较低,则应将其转换为较高的字符;如果该字符较高,则应将其转换为较低的字符???当您希望每秒钟擦洗一次字符大小写时,请尝试以下操作:Bash 将可选字符从低级改为高级,从高级改为低级-Unix shell脚本,bash,unix,Bash,Unix,如何转换传递给脚本的字符串的可选字符,如果该字符较低,则应将其转换为较高的字符;如果该字符较高,则应将其转换为较低的字符???当您希望每秒钟擦洗一次字符大小写时,请尝试以下操作: read -p " Enter string" str for i in `seq 0 ${#str}` do #echo $i rem=$(($i % 2 )) if [ $rem -eq 0 ] then echo ${str:$i:1} else fr=${str:$i:1} if [[ "$fr" =
read -p " Enter string" str
for i in `seq 0 ${#str}`
do
#echo $i
rem=$(($i % 2 ))
if [ $rem -eq 0 ]
then
echo ${str:$i:1}
else
fr=${str:$i:1}
if [[ "$fr" =~ [A-Z] ]]
then
echo ${str:$i:1} | tr '[:upper:]' '[:lower:]'
elif [[ "$fr" =~ [a-z] ]]
then
echo ${str:$i:1} | tr '[:lower:]' '[:upper:]'
else
echo ""
fi
fi
done
read -p " Enter string " str
for i in `seq 0 ${#str}`; do
rem=$(($i % 2 ))
if [ $rem -eq 0 ]
then
printf "%s" "${str:$i:1}"
else
fr=${str:$i:1}
printf "%s" "$(tr '[:upper:][:lower:]' '[:lower:][:upper:]' <<< "${str:$i:1}")"
fi
done
echo
read-p“输入字符串”str
对于'seq 0${str}'中的i;做
rem=$($i%2))
如果[$rem-等式0]
然后
printf“%s”${str:$i:1}
其他的
fr=${str:$i:1}
printf“%s”“$(tr'[:upper:][:lower:][:upper:][:lower:][:upper:][p>您的问题有点挑战性,因为它被标记为shell
,而不是与bash或zsh之类的高级shell相关的问题。在POSIX shell中,您没有字符串索引,没有用于循环的C样式,也没有[..]
运算符使用字符类模式匹配
然而,有点笨拙的创意,旧的expr
和POSIX字符串和算术运算,并将字符串限制为ASCII字符,您可以在字符串上迭代,将大写字母改为小写字母,小写字母和大写字母,同时保持所有其他字符不变
如果您有一个高级shell可用,我不推荐这种方法,但是如果您仅限于POSIX shell,因为您的问题被标记了,它会起作用,但不要期望它会非常快
#!/bin/sh
a=${1:-"This Is My 10TH String"} ## input and output strings
b=
i=1 ## counter and string length
len=$(expr length "$a")
asciiA=$(printf "%d" "'A") ## ASCII values for A,Z,a,z
asciiZ=$(printf "%d" "'Z")
asciia=$(printf "%d" "'a")
asciiz=$(printf "%d" "'z")
echo "input : $a" ## output original string
while [ "$i" -le "$len" ]; do ## loop over each character
c=$(expr substr "$a" "$i" "1") ## extract char from string
asciic=$(printf "%d" "'$c") ## convert to ASCII value
## check if asciic is [A-Za-z]
if [ "$asciiA" -le "$asciic" -a "$asciic" -le "$asciiZ" ] ||
[ "$asciia" -le "$asciic" -a "$asciic" -le "$asciiz" ]
then ## toggle the sign bit (bit-6)
b="${b}$(printf "\x$(printf "%x" $((asciic ^ 1 << 5)))\n")"
else
b="$b$c" ## otherwise copy as is
fi
i=$(expr $i + 1)
done
echo "output: $b" ## output resluting string
问题是什么?当我将字符串传递给脚本时,它应该将可选字符从下到上或从上到下更改。我仍然没有看到问题。提示:问题是以问号结尾的句子。更新了问题检查out@GudalaSandeep请看:。选择您认为有助于您完成任务的答案st并标记已回答的问题。如果您尚未收到回答问题的答案,请澄清您的问题。选择谁的答案并不重要,重要的是选择一个答案,这样此问题就不会继续在未回答的队列中跳转。堆栈溢出时,您可以修改自己的问题。请将此代码添加到qu除非它被重新标记为高级shell,而不是简单的shell
,否则POSIX shell中没有字符串索引,也没有[[…]]]
,例如${str:$i:1}
(两者都是bashism)没有字符串索引(${str:$i:1}
)在POSIX shell中,添加了另一个bash解决方案在阅读OP给出的答案后,我意识到他不想切换每个字符,而只想切换每一个字符。哦,你在解释方面比我做得更好。我只是得到了“如果是较低的,那么是较高的,如果是较高的,那么是较低的……”的答案“如何转换字符串的可选字符”在第1、2、3等问题上对我来说完全是模棱两可的。有趣的问题-在C中简单,在shell中不那么简单。在高级shell中更好更快。C代码看起来就像我使用grep-o.
读取1个字符的地方(我可以使用IFS=read-r-n1-d''onechar
)。这些是POSIX吗?回答得很好。POSIX没有((I++))
作为一个独立的组件,没有任何IFS
,当然也没有字符串索引。最好的参考是,尽管几乎所有的shell都在纯POSIX一致性之外添加了一些东西。POSIX规范只是提供了一个最小公分母以确保可移植性。它很好地指出了POSIX问题。
#!/bin/sh
a=${1:-"This Is My 10TH String"} ## input and output strings
b=
i=1 ## counter and string length
len=$(expr length "$a")
asciiA=$(printf "%d" "'A") ## ASCII values for A,Z,a,z
asciiZ=$(printf "%d" "'Z")
asciia=$(printf "%d" "'a")
asciiz=$(printf "%d" "'z")
echo "input : $a" ## output original string
while [ "$i" -le "$len" ]; do ## loop over each character
c=$(expr substr "$a" "$i" "1") ## extract char from string
asciic=$(printf "%d" "'$c") ## convert to ASCII value
## check if asciic is [A-Za-z]
if [ "$asciiA" -le "$asciic" -a "$asciic" -le "$asciiZ" ] ||
[ "$asciia" -le "$asciic" -a "$asciic" -le "$asciiz" ]
then ## toggle the sign bit (bit-6)
b="${b}$(printf "\x$(printf "%x" $((asciic ^ 1 << 5)))\n")"
else
b="$b$c" ## otherwise copy as is
fi
i=$(expr $i + 1)
done
echo "output: $b" ## output resluting string
$ sh togglecase.sh
input : This Is My 10TH String
output: tHIS iS mY 10th sTRING