簡體   English   中英

在UNIX bash shell腳本中創建“是”或“否”菜單

[英]Creating a 'yes' or 'no' menu in UNIX bash shell scripting

我正在編寫一個腳本,有一次我想檢查一個文件是否已經存在。 如果文件不存在,那么它什么都不做。 但是,如果文件確實存在,我想要出現'y'或'n'(是或否)菜單。 它應該詢問“你想覆蓋這個文件嗎?”。

到目前為止,我已經嘗試過類似的東西。 在此之前考慮一個函數:

therestore

存在。 如果鍵入“y”,我希望發生此功能。 無論如何,這是我嘗試過的:

If [ -f directorypathANDfilename ] ; then
 read -p "A file with the same name exists, Overwrite it? Type y/n?" yesorno
    case $yesorno in
            y*)  therestore ;;
            n*)  echo "File has not been restored" ;;
    esac
fi

但是出於某種原因,菜單總是會彈出,即使文件不存在,如果我鍵入是,它也無法正常恢復! (但我知道“therestore”功能正常,因為我已經測試了很多次)。

為冗長的問題道歉。 如果您需要更多詳細信息,請告知我們 - 提前感謝!

你的腳本甚至運行了嗎? 對我來說看起來不像是有效的bash-script。 If不是有效的關鍵字,但if是。 此外,測試進入尖括號[ ] ,這些不是可選的。 此外,您忘了關閉fi
還有一件事,我不太清楚你在測試什么。 directorypathANDfilename是變量嗎? 在這種情況下,你必須用$引用它。

該片段可能會更好地工作:

#!/bin/bash

if [ -f "$directorypathANDfilename" ] ; then
 read -p "A file with the same name exists, Overwrite it? Type y/n?" yesorno
    case "$yesorno" in
            y*)  therestore ;;
            n*)  echo "File has not been restored" ;;
    esac
fi

暫無
暫無

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

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