删除 bashism 以移植到 shell

Remove bashism to port into shell

我遇到了麻烦:在纯 Bourne Shell 中转换此 BASH 片段的方法是什么?

if [[ -d "$d" ]] && [[ ! -L "$d" ]] && [[ "$OLD_PATH" =~ $d ]]; then
 ...omissis ...
fi

这是完整的脚本:

if [ "$OLD_PATH" ]; then
  render_text "info" "PATH" "$OLD_PATH"
  
  # check if some writable folder is in the PATH
  wr_folder_in_path="";
  
  OLD_IFS=$IFS
  IFS=$'\n'
  for d in $writable_folders; do  
    if [[ -d "$d" ]] && [[ ! -L "$d" ]] && [[ "$OLD_PATH" =~ $d ]]; then
      if [ "$wr_folder_in_path" ]; then wr_folder_in_path="$wr_folder_in_path\|$d"; else wr_folder_in_path="$d"; fi
    fi
  done
  IFS=$OLD_IFS
  
  PATH_W_SPACES=`echo "$OLD_PATH" | tr ':' ' '`
  pathswriteable=`(ls -dlah $PATH_W_SPACES | sed "s,$wr_folder_in_path,${_sed_green},g") 2> /dev/null`
  if [ "$pathswriteable" ]; then
    render_text "warning" "Check if some folder of the PATH is ${_green}writable" "$pathswriteable"
  fi
fi

我得到:

./LE.sh: 636: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-17-oracle/bin:/usr/lib/jvm/java-17-oracle/db/bin: not found

这是从 https://github.com/filippolauria/LinEnum

移植 LinEnum 脚本
if [ -d "$d" ]; then
 if [ ! -L "$d" ]; then
  if "$OLD_PATH" | grep -Eq "$d"; then

变量不是你可以执行的。您应该将变量的内容打印到 grep.

if
   [ -d "$d" ] &&
   [ ! -L "$d" ] &&
   printf "%s" "$OLD_PATH" | grep -q "$d"
then

我会考虑将 -F 添加到 grep 以按字面匹配它,或者使用 case - 该代码段很可能无法按预期使用包含 [ [=15= 的路径] + *.