haveuppernumber()
{
#test if the string have upper number
str="$(echo $1 | tr '[:upper:]' '[:lower:]')"
if [ "$str" != $1 ] ; then #get some problem
echo "[#have upper number,and i well trun them to lower:#]"
return 1 #have upper number
else
return 0 #no upper number
fi
}
if [ $# -ne 1 ] ; then
echo "Usage: $0 <string>" >&2
exit 1
fi
if ! haveuppernumber $1 ; then #when if is 0 it run?
#if [ 0 ] ; then #in shell true return 0 ,false return 1
echo $1 | tr '[:upper:]' '[:lower:]' #it can turn the UPPER number to lower
# echo $1 | tr '[:lower:]' '[:upper:]' #it can turn the lower number to UPPER
else
echo "[#no upper number:#]"
echo $1
fi
exit 0
功能說明:當輸入”./upper_to_lower.sh AaBbCcdd“時會先判斷輸入格式是否正確,然後判斷字符串中是否有大寫字母如果有顯示"[#have upper number,and i well trun them to lower:#]"和轉換成小寫字母後的字符串;如果沒有大寫字母顯示"[#no upper number:#]"和小寫字符串。
然後又試著用c語言實現相同的功能,如下:
復制代碼 代碼如下:int haveuppernumber(char *p)
{
char*q=p;
for(;*q!=' ';q++)
{
if(*q>='A'&&*q<='Z')
{
printf("[#have upper number and i will turn them to lower #]n");
return 1;
}
}
printf("[#no upper number#]n");
return 0;
}
void turntolower(char *p)
{
for(;*p != ' ';p++)
{
if(*p>='A' && *p<='Z')
{
*p+=' ';
}
}
}
int main(int argc , char *argv[])
{
char *p;
p=argv[1];
if(argc != 2)
{
printf("Usage : %s <string>n",argv[0]);
exit(-1);
}
if(haveuppernumber(p))
{
turntolower(p);
printf("%sn",argv[1]);
}
else
{
printf("%sn",argv[1]);
}
return 0;
}