Bash反击逃脱

时间:2013-03-21 01:16:59

标签: bash

这是Bash变量声明语句:

foo='b'`    `'ar'

效率比这个:

foo='bar'

这种格式化“悬挂”或“对齐”缩进的方式(行连续):

a_long_variable_name='a seemingly infinitely long '`
                    `'string requiring continuatio'`
                    `'n on the next line...'
除了之外,

以任何方式产生子shell,浪费资源或影响性能 烦恼那些不喜欢(反?)可读性的人?

效率如下:计算机效率,而不是“人类输入许多字符并创造基于空间的,无偿的维护债务”。

是否可以轻松证明对性能的影响(或缺乏影响)?

2 个答案:

答案 0 :(得分:5)

tester1() {
  for i in {1..1000}; do
    a_long_variable_name='a seemingly infinitely long '`
                        `'string requiring continuation'`
                        `'on the next line...'
    echo $a_long_variable_name > tmp 
  done
}

tester2() {
  for i in {1..1000}; do
    a_long_variable_name="a seemingly infinitely long\
string requiring continuation\
on the next line..."
    echo $a_long_variable_name > tmp 
  done
}

echo tester1
time tester1
echo tester2
time tester2

<强>结果

tester1

real    0m1.878s
user    0m0.209s
sys     0m0.566s

tester2

real    0m0.335s
user    0m0.026s
sys     0m0.078s

这些都与案例2具有相似的时间:

read -r -d '' a_long_variable_name <<EOF
    a seemingly infinitely long
    string requiring continuation
    on the next line...
EOF

a_long_variable_name="a seemingly infinitely long\
                      string requiring continuation\
                      on the next line..."

答案 1 :(得分:0)

如果我需要它,我可能会建立字符串:

x='a seemingly infinitely long '
x="$x"'string requiring continuatio'
x="$x"'n on the next line...'

a_long_variable_name="$x"

或该主题的微小变化。或者我会在每一行重复长变量名称。这是我使用的真实脚本 - 它为我列出了相关的环境变量:

informix1="DB[^=]|DELIMIDENT=|SQL|ONCONFIG|TBCONFIG|INFOR"
informix2="CLIENT_LOCALE=|GL_|GLS8BITSYS|CC8BITLEVEL|ESQL|FET_BUF_SIZE="
informix3="INF_ROLE_SEP=|NODEFDAC=|ONCONFIG|OPTCOMPIND|PDQ|PSORT"
informix4="PLCONFIG|SERVER_LOCALE|FGL|C4GL|NE_"
informix5="TCL_LIBRARY|TK_LIBRARY|TERM=|TERMCAP=|TERMINFO="
informix="$informix1|$informix2|$informix3|$informix4|$informix5"
system1="COLLCHAR=|LANG=|LC_"
system2="(DY)?LD_LIBRARY_PATH(_[63][42])?=|PATH=|SHLIB_PATH=|LIBPATH="
system="$system1|$system2"
jlss="IX([A-Z]|D(32|64)?)="

env |
${EGREP:-egrep} "^($informix|$system|$jlss)" |
sort

(当Mac OS X 10.7.x egrep因为“太大”而停止处理表达式时,能够切换使用哪个egrep程序是必要的。)