File Tests (-f -d -e) का उपयोग
File tests यह देखने के लिए खिड़की से झांकने जैसे हैं कि एक घर है या नहीं, और यह घर है या बगीचा। आप पूछ सकते हैं कि कुछ exist करता है, एक file है, या एक folder है।
In this page:
Syntax
[[ -f path ]]
[[ -d path ]]
[[ -e path ]]
File Tests (-f -d -e)
[[ -f path ]] जांचता है कि एक path मौजूद है और एक regular file है या नहीं, [[ -d path ]] एक directory के लिए जांचता है, और [[ -e path ]] दोनों में से किसी के भी existence के लिए जांचता है।
ये conditional file tests एक path पर पढ़ने, लिखने, या act करने से पहले essential हैं जिसके बारे में आप निश्चित नहीं कि मौजूद है। Opposite test करने के लिए इन्हें ! के साथ combine करें, जैसे [[ ! -f path ]]।
Note:
-eजांचता है "कुछ भी exist करता है"-fऔर-dइस बारे में ज़्यादा specific हैं कि यह किस तरह की चीज़ है।
उदाहरण: File Tests (-f -d -e)
#!/bin/bash
touch sample.txt
mkdir -p sample_dir
if [[ -f sample.txt ]]; then
echo "sample.txt is a regular file"
fi
if [[ -d sample_dir ]]; then
echo "sample_dir is a directory"
fi
if [[ -e nonexistent.txt ]]; then
echo "This won't print"
else
echo "nonexistent.txt does not exist"
fi
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
[ -f "$file" ]में spaces भूल जाना।-fया-dचाहिए होने पर-eउपयोग करना, क्योंकि-eकिसी भी type के लिए true है।- Variable को unquoted छोड़ना, जो spaces वाले names के साथ fail होता है।
🔒
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: