问题
I have a shell script that starts unit tests for modules. I need the name of the module in all lowercase and with the first character uppercase. So far I have been doing it like this:
#!/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
What would be an easier way to set $strModuleToTestUppercase? I tried
strModuleToTestUppercase="${strModuleToTest[@]^}"
or
strModuleToTestUppercasesed="sed 's/\<./\u&/g' $strModuleToTest"
but without success.
回答1:
If:
s=somemodule
with bash v4+
echo ${s^}
This should work with a bit older bash versions (from Glenn):
echo $(tr a-z A-Z <<< ${s:0:1})${s:1}"
with zsh
echo ${(C)s}
with ash and coreutils
echo $(echo $s | cut -c1 | tr a-z A-Z)$(echo $s | cut -c2-)
with GNU sed
echo $s | sed 's/./\U&/'
with BSD sed
echo $s | sed '
h;
y/quvwxzdermatoglyphicsbfjkn/QUVWXZDERMATOGLYPHICSBFJKN/;
G;
s/\(.\)[^\n]*\n.\(.*\)/\1\2/;
'
with awk
echo $s | awk '{ print toupper(substr($0, 1, 1)) substr($0, 2) }'
with perl
echo $s | perl -nE 'say ucfirst'
with python
echo $s | python -c 'import sys; print sys.stdin.readline().rstrip().capitalize()'
with ruby
echo $s | ruby -e 'puts ARGF.read.capitalize'
Output in all cases
Somemodule
回答2:
You could use
strModuleToTestUpper=`sed 's/\(.\)/\U\1/' <<< "$strModuleToTest"`
Explanation
\(.\)
matches a single character\U\1
replaces that character with an uppercase version- no
/g
means only the first match is processed.
回答3:
Is perl ok?
$ x=foobar
$ x=$(echo "$x" | perl -pe 's/^(.)/uc($1)/e')
$ echo $x
Foobar
来源:https://stackoverflow.com/questions/12420317/first-character-of-a-variable-in-a-shell-script-to-uppercase