shell脚本中变量的第一个字符是否为大写?

时间:2012-09-14 07:57:43

标签: shell unix sed awk

我有一个shell脚本,可以启动模块的单元测试。我需要全部小写的模块名称和第一个字符大写。到目前为止,我一直在这样做:

#!/bin/sh -x                                                                                                                                                                          
# z.B. getbrowser                                                                                                                                                                     
strModuleToTest=$1

# g                                                                                                                                                                                   
strModuleToTestUppercaseFirstletter=${strModuleToTest:0:1}
# etbrowser                                                                                                                                                                           
strModuleToTestUppercaseLastletters=${strModuleToTest:1}

# g -> G                                                                                                                                                                              
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/a/A}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/b/B}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/c/C}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/d/D}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/e/E}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/f/F}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/g/G}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/h/H}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/i/I}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/j/J}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/k/K}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/l/L}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/m/M}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/n/N}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/o/O}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/p/P}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/q/Q}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/r/R}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/s/S}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/t/T}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/u/U}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/v/V}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/w/W}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/x/X}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/y/Y}"
strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/z/Z}"

# Getbrowser
strModuleToTestUppercase=$strModuleToTestUppercaseFirstletter""$strModuleToTestUppercaseLastletters

设置$ strModuleToTestUppercase更简单的方法是什么?我试过了

strModuleToTestUppercase="${strModuleToTest[@]^}"

strModuleToTestUppercasesed="sed 's/\<./\u&/g' $strModuleToTest"

但没有成功。

3 个答案:

答案 0 :(得分:29)

如果:

s=somemodule

使用bash v4 +

echo ${s^}

这应该适用于较旧的bash版本(来自Glenn):

echo $(tr a-z A-Z <<< ${s:0:1})${s:1}"

使用zsh

echo ${(C)s}

使用ash和coreutils

echo $(echo $s | cut -c1 | tr a-z A-Z)$(echo $s | cut -c2-)

使用GNU sed

echo $s | sed 's/./\U&/'

与BSD sed

echo $s | sed '
  h;
  y/quvwxzdermatoglyphicsbfjkn/QUVWXZDERMATOGLYPHICSBFJKN/;
  G;
  s/\(.\)[^\n]*\n.\(.*\)/\1\2/;
'

使用awk

echo $s | awk '{ print toupper(substr($0, 1, 1)) substr($0, 2) }'

with perl

echo $s | perl -nE 'say ucfirst'

所有情况下的输出

Somemodule

答案 1 :(得分:10)

您可以使用

strModuleToTestUpper=`sed 's/\(.\)/\U\1/' <<< "$strModuleToTest"`

解释

  • \(.\)匹配单个字符
  • \U\1将该字符替换为大写版本
  • no /g表示只处理第一个匹配。

答案 2 :(得分:1)

perl好吗?

$ x=foobar
$ x=$(echo "$x" | perl -pe 's/^(.)/uc($1)/e')
$ echo $x
Foobar