如果目录中的文件数大于文件第一行中的数,则打印“hello”

Mar*_*oah 3 command-line bash

如果当前目录中的文件数大于文件检查第一行中指定的数量,则编写打印 hello 的命令行。

这工作正常,但我想要一个命令行。有任何想法吗?

firstline=$(head -1 check)
allfiles=$(ls | wc -l)
echo $allfiles  $firstline

if (($allfiles > $firstline)); then
     echo "hello"
else 
     echo "oh no"
fi
Run Code Online (Sandbox Code Playgroud)

hee*_*ayl 5

您可以使用这种衬垫:

files=( * ); [[ ${#files[@]} -gt $(head -1 check) ]] && echo 'hello' || echo 'oh no'
Run Code Online (Sandbox Code Playgroud)

files数组将包含当前目录的文件,因此${#files[@]}显示数组中的元素,即当前目录中的文件数。

check第一行是数字的文件被提取出来head -1 check

这是扩展形式:

最后,如果文件数大于check( [[ ${#files[@]} -gt $(head -1 check) ]])第一行的数字,hello则打印。

这是扩展形式:

#!/bin/bash
files=( * )
if [[ ${#files[@]} -gt $(head -1 check) ]]; then
    echo 'hello'
else
    echo 'oh no'
fi
Run Code Online (Sandbox Code Playgroud)