如果目錄中的檔案數量大於檔案第一行的數量,則列印“hello”

如果目錄中的檔案數量大於檔案第一行的數量,則列印“hello”

編寫命令行,如果目前目錄中的檔案數量大於檔案檢查第一行中指定的數量,則列印 hello。

這工作正常,但我想要一個命令行。有任何想法嗎?

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

if (($allfiles > $firstline)); then
     echo "hello"
else 
     echo "oh no"
fi

答案1

您可以使用這款襯墊:

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

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

相關內容