簡體   English   中英

在簡單的 bash 腳本中使用 find 命令和 if 語句

[英]Use find command and if statement in a simple bash script

我在/root/xml-test/中有一個目錄,其中包含.xml文件,每個文件大小為 99MB。

statement to check if the size is larger than $a .我正在使用if 語句來檢查大小是否大於$a

問題是...... if 語句總是導致 A 大於 B - 即使我為 A 選擇了一個較小的數字 - 例如 1。

這是測試自己的腳本。 評論中有一個 URL 可以為您的測試創建任意大小的文件。

#!/bin/bash

#This will check the size of the XML files in /root/xml-test


# Script Test Procedures
# Create file of any size
#https://www.ostechnix.com/create-files-certain-size-linux/

xmlpath='/root/xml-test/.'

size=$(find $xmlpath -type f -name '*.xml' -exec du -c {} + | grep total$) ; total=$(echo $size |  cut -c 1- | rev | cut -c 7- | rev)

b=$total
echo $total 'size in blocks' &&  echo "enter a second number";'

read a ;
echo "a=$a";
echo b=$total;

if [ $a > $b ];
then
    echo "a is greater than b";
else
    echo "b is greater than a";
fi;

在@gordon-davisson 的幫助下,該問題的解決方案正在改變以下內容:

if [ $a > $b ];

if [ $a \> $b ];

A 現在大於 B

B 可以大於 A

很好!

正確的解決方案是 integer 與 integer 比較:

[ "$a" -gt "$b" ]

手冊頁將帶有-gt選項的test命令定義為:

   INTEGER1 -gt INTEGER2
          INTEGER1 is greater than INTEGER2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM