Convert the following bash script to sh.

huangapple go评论69阅读模式
英文:

Convert the follwing bash script to sh

问题

Here's the translated script in sh without the need for any additional explanations:

#!/bin/sh
find ~/project/jobs -maxdepth 1 -mindepth 1 -type d ! -name "utils" -exec basename {} \; | tr '\n' ',' | sed 's/,$//' | (IFS=','; read -r -a ary; echo "[${ary[*]}]")

Please note that this script has been adapted to work in sh, addressing the issues you mentioned.

英文:

I have this script:

#!/bin/sh
while IFS= read -r -d "" f; do
    ary+=("$f")
done < <(find ~/project/jobs -maxdepth 1 -mindepth 1 -type d -not -name "utils" -printf "\"%f\"
#!/bin/sh
while IFS= read -r -d "" f; do
ary+=("$f")
done < <(find ~/project/jobs -maxdepth 1 -mindepth 1 -type d -not -name "utils" -printf "\"%f\"\0")
(IFS=","; echo "[${ary[*]}]")
") (IFS=","; echo "[${ary[*]}]")

That is supposed to list the top level folder of the my_folder except the one named exclude.
It is made in bash, but I don't have bash on the machine I'm executing it.

Which result in giving me the error :

>(On line 3) Syntax error: word unexpected (expecting ")")

I would like to convert it in sh so I would be able to execute it.

I put it on https://www.shellcheck.net/

and it gave me the errors :

Line 4:
while IFS= read -r -d "" f; do
                   ^-- SC2039: In POSIX sh, read -d is undefined.
 
Line 5:
    ary+=("$f")
    ^-- SC2039: In POSIX sh, += is undefined.
         ^-- SC2039: In POSIX sh, arrays are undefined.
 
Line 6:
done < <(find ~/project/jobs -maxdepth 1 -mindepth 1 -type d -not -name "utils" -printf "\"%f\"
Line 4:
while IFS= read -r -d "" f; do
^-- SC2039: In POSIX sh, read -d is undefined.
Line 5:
ary+=("$f")
^-- SC2039: In POSIX sh, += is undefined.
^-- SC2039: In POSIX sh, arrays are undefined.
Line 6:
done < <(find ~/project/jobs -maxdepth 1 -mindepth 1 -type d -not -name "utils" -printf "\"%f\"\0")
^-- SC2039: In POSIX sh, process substitution is undefined.
Line 8:
(IFS=","; echo "[${ary[*]}]")
^-- SC2039: In POSIX sh, array references are undefined.
") ^-- SC2039: In POSIX sh, process substitution is undefined. Line 8: (IFS=","; echo "[${ary[*]}]") ^-- SC2039: In POSIX sh, array references are undefined.

Is there a way to convert it to sh ? How would I proceed.

答案1

得分: 2

You don't really need find for this at all. Since POSIX doesn't support arbitrary arrays, you can use the positional parameters in their place.


#!/bin/sh

set --   # 清空位置参数
cd ~/project/jobs

for d in */; do
  [ "$d" = "utils/" ] && continue
  set -- "$@" "\"${d%/}\""
done

(IFS=","; printf '[%s]\n' "$*")

If the script is going to exit immediately, you can drop the parentheses, as there won't be any harm in setting `IFS` globally.

As it appears you are trying to generate JSON, I would recommend the following if installing jq is at all an option:

cd ~/project/jobs
jq -n --args '$ARGS.positional | select(.!="utils/") | map(rtrimstr("/"))' */
英文:

You don't really need find for this at all. Since POSIX doesn't support arbitrary arrays, you can use the positional parameters in their place.

#!/bin/sh

set --   # Clear the positional parameters
cd ~/project/jobs

for d in */; do
  [ "$d" = "utils/" ] && continue
  set -- "$@" "\"${d%/}\""
done

(IFS=","; printf '[%s]\n' "$*")

If the script is going to exit immediately, you can drop the parentheses, as there won't be any harm in setting IFS globally.


As it appears you are trying to generate JSON, I would recommend the following if installing jq is at all an option:

cd ~/project/jobs
jq -n --args '$ARGS.positional | select(.!="utils/") | map(rtrimstr("/"))' */

答案2

得分: 1

以下是翻译好的代码部分:

#!/bin/sh
find ~/project/jobs -maxdepth 1 -mindepth 1 -type d -not -name "utils" -printf "\"%f\"\n" | { \
sep=
while read -r f; do
    ary="$ary$sep$f"
    sep=,
done
echo "[$ary]"
}

说明

与处理替代方法不同,我们使用管道(|)。

此外,我们需要将 while 循环与最终的 echo 分组。

英文:

If we just wanted to convert the original script, here it is :

#!/bin/sh
find ~/project/jobs -maxdepth 1 -mindepth 1 -type d -not -name "utils" -printf "\"%f\"\n" | { \
sep=
while read -r f; do
    ary="$ary$sep$f"
    sep=,
done
echo "[$ary]"
}

Explain

Instead of process substitution, we use piping (|).

Also we have to group while-loop with the final echo.

huangapple
  • 本文由 发表于 2020年1月6日 21:38:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定