17

我有一个启动模块单元测试的 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"

但没有成功。

4

3 回答 3

46

如果:

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) }'

用 perl

echo $s | perl -nE 'say ucfirst'

用蟒蛇

echo $s | python -c 'import sys; print sys.stdin.readline().rstrip().capitalize()'

红宝石

echo $s | ruby -e 'puts ARGF.read.capitalize'

所有情况下的输出

Somemodule
于 2012-09-14T10:09:11.257 回答
10

你可以使用

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

解释

  • \(.\)匹配单个字符
  • \U\1用大写版本替换该字符
  • no/g表示只处理第一个匹配项。
于 2012-09-14T08:10:58.533 回答
1

perl 可以吗?

$ x=foobar
$ x=$(echo "$x" | perl -pe 's/^(.)/uc($1)/e')
$ echo $x
Foobar
于 2012-09-14T08:15:41.453 回答