检查var

时间:2017-04-10 08:34:16

标签: bash server

我尝试列出/etc/nginx/html/中的所有文件夹。

我使用此代码:

folders=`for i in $(ls -d */ | grep -Ev "(backups)"); do echo ${i%%/}; done`

现在我得到名字不包含"备份"。

的所有文件夹

现在我想计算文件夹并将其保存到var中。我使用这段代码:

folders_count=( $folders )
echo ${#folders_count[@]}

有效。我得到了所有文件夹的列表,我可以计算文件夹。

现在我尝试编写一个读取用户输入的脚本(或者更好,提供1,2,3个选项,但我不知道该怎么做)并安装像Prestashop这样的脚本(作为示例)到文件夹中用户已选择是否有多个文件夹。

这是我的代码:

folders_count=( $folders )
echo ${#folders_count[@]}
echo
if [ ${#folders_count[@]} -gt 1 ]; then
  echo "${info} Please choose a Website for Prestashop:" | awk '{ print strftime("[%H:%M:%S] |"), $0 }'
  # for websites in $folders; do
  # echo "${info} $folders" | awk '{ print strftime("[%H:%M:%S] |"), $0 }'
  # echo "${info} $folders" | awk '{ print strftime("[%H:%M:%S] |"), $0 }'
  # done
  echo "$folders" | awk '{ print strftime("[%H:%M:%S] |"), $0 }'
  #read websites_for_prestashop
  read -e -i "$websites_for_prestashop" -p "Please enter your Website: " input
  websites_for_prestashop="${input:-$websites_for_prestashop}"
  if [ "$websites_for_prestashop" == "$folders" ]; then
    echo "Website is $websites_for_prestashop"
  else
    echo "Website $websites_for_prestashop is not found"
  fi
  #echo "${info} Please choose a Website for Prestashop:" | awk '{ print strftime("[%H:%M:%S] |"), $0 }'
else
  echo "nix"
fi

我不知道如何让它发挥作用:/

有人可以帮我解决吗?

我想列出可用的文件夹。然后用户应该能够选择他想要在哪个文件夹中安装脚本。

文件夹:

[0] Folder 1
[1] Folder 2
[2] Folder 3
[3] Folder 4
....

然后会出现一个查询: 请选择安装订单:

然后应将文件夹的选择存储在变量read websites_for_prestashop

2 个答案:

答案 0 :(得分:0)

我不知道你的剧本应该做什么。注释掉了一堆行,websites_for_prestashop的唯一赋值包含对变量本身的引用,并且在首次使用此变量之后发生,并且脚本最终不执行任何操作。

我可以告诉你的是,提取文件名的方式不是正确的方法。解析ls的输出并不是一个好主意(参见http://mywiki.wooledge.org/ParsingLs),并且对于名称中包含空格的任何文件,其执行方式都会失败。

这是一种方法:

declare -a folders=()
for f in */
do
  [[ $f =~ backups ]] || folders+=("${f%/}")
done
echo "Folder count is: ${#folders[@]}"

答案 1 :(得分:0)

谢谢你的帮助@ https://stackoverflow.com/users/2416376/olli-k

https://stackoverflow.com/users/7422249/fred

解决方案很简单:

folders=`for i in $(ls -d */ | grep -Ev "(backups)"); do echo ${i%%/}; done`

        echo "${info} Please choose a Website for Prestashop:" | awk '{ print strftime("[%H:%M:%S] |"), $0 }'
        select websites_for_prestashop in $folders;
        do
             echo "You picked $websites_for_prestashop."
             break
        done