我有这个字符串
Alphabet = ABCDEFGHIJKLMNOPQRSTUVWXYZ
我希望让用户输入一个特定的单词来删除字母表中的字母;但是一旦我得到输入,我就不知道如何真正删除字符串中的字母,使其大小最小化为1。我写了以下代码:
#!/bin/bash
Alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
echo -n "Please enter your key: "
read -e KEY
Asize=`expr ${#Alphabet} - 1`
Ksize=`expr ${#KEY} - 1`
kcount=0
#A loop to go through the key characters
while [ $kcount -le $Ksize ]
do
D=${KEY:$kcount:1}
Acount=0
#A loop to go through the alphabet characters to look for the current key letter to delete it
while [ $Acount -le $Asize ]
do
if [ "${KEY:$kcount:1}" == "${Alphabet:$Acount:1}" ];
then
**REMOVING PART**
break
else
Acount=$[$Acount+1]
如果有人知道我怎么做,我真的很感激他的帮助。 谢谢。示例如下:
输入:CZB 输出:
Kcount = 0:ABDFGHIJLMNOPQRSTUVWXYZ
Kcount = 1:ABDFGHIJLMNOPQRSTUVWXY
Kcount = 2:ADFGHIJLMNOPQRSTUVWXY
答案 0 :(得分:3)
$ foo="bar"
$ echo "${foo/a/}"
br
答案 1 :(得分:0)
使用tr
实用程序:
SYNOPSIS
tr [-Ccu] -d string1
…
DESCRIPTION
The tr utility copies the standard input to the standard output with sub-
stitution or deletion of selected characters.
…
-d Delete characters in string1 from the input.
因此
#!/bin/bash
Alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
echo -n "Please enter your key: "
read -e KEY
echo "$Alphabet" | tr -d "$KEY"
如果您在提示符下键入ADFGHIJLMNOPQRSTUVWXY
,会生成输出CBZ
。