【问题标题】:Remove bashism to port into shell [closed]删除 bashism 以移植到 shell [关闭]
【发布时间】:2022-01-10 09:27:11
【问题描述】:

我遇到了麻烦:有什么方法可以在纯 Bourne Shell 中转换这个 BASH sn-p?

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 脚本

【问题讨论】:

  • 有什么问题?
  • 我无法转换它。我尝试使用pastebin.com/LwbC8Rz8,但它没有按预期工作。其实我并没有取得好的成绩,我必须承认我在纯bash方面没有那么经验......
  • 如果您可以粘贴整个 shell 脚本,那将非常有帮助并且您会遇到错误。
  • 完整的 sn-p 是 pastebin.com/4UQFmvca,我得到的错误是 "./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 " 其中 636 是调用 sn-p 的行。这是从 github.com/filippolauria/LinEnum 移植的 LinEnum 脚本。在原始脚本中是@ 592 行
  • 您能详细说明您想要实现的目标吗?

标签: bash shell script


【解决方案1】:
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 - sn-p 很可能不会像预期的那样与包含 [ ] + * 的路径一起工作。

【讨论】:

  • 我非常感谢,但如果 greep 成功了,我需要将 $d 放入一个 wariable 中:检查 pastebin.com/FTWB2q1R 这样我就不必将它打印出来......任何关于如何实现这一点的建议?
  • 当然,就这么做。
  • 我把它变成了if [ -d "$d" ]; then if [ ! -L "$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;fi,但还是有问题。以下是不同之处:pastebin.com/z5JyxzRu 任何进一步的建议?
  • 我用pastebin.com/vU0BRqPg解决了你的提示
猜你喜欢
  • 2022-09-23
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-08
相关资源
最近更新 更多