我有一台ubuntu机器,其默认shell设置为bash,并且两种方式都设置为$PATH中的二进制文件:
$which bash
/bin/bash
$which sh
/bin/sh
$ll /bin/sh
lrwxrwxrwx 1 root root 4 Mar 6 2013 /bin/sh -> bash*
但是,当我尝试调用使用the inline file descriptor的脚本(只有bash可以处理,但sh不能处理)时,两个调用的行为不同:
$. ./inline-pipe
reached
$bash ./inline-pipe
reached
$sh ./inline-pipe
./inline-pipe: line 6: syntax error near unexpected token `
./inline-pipe: line 6: `done <
我指的示例脚本看起来像这样
#!/bin/sh
while read line; do
if [[ "$line" == "reached" ]]; then echo "reached"; fi
done <
真正的更长一点:
#!/bin/sh
declare -A elements
while read line
do
for ele in $(echo $line | grep -o "[a-z]*:[^ ]*")
do
id=$(echo $ele | cut -d ":" -f 1)
elements["$id"]=$(echo $ele | cut -d ":" -f 2)
done
done <
echo ${elements[*]}