test और [ ] Operators
[ ] (और equivalent test command) conditions जांचने का classic, POSIX-standard तरीका है, जैसे दो strings बराबर हैं या एक number दूसरे से बड़ा है।In this page:
[ "$value1" -eq "$value2" ]
[ "$string1" = "$string2" ]
test expression
[ ] असल में test Command है
[ expression ] literally test command को एक call है, जहां [ एक program/builtin है जिसे अपने final argument के रूप में एक matching ] चाहिए।
यही कारण है कि दोनों brackets के आसपास spaces mandatory हैं -- वे separate arguments हैं, decorative syntax नहीं।
उदाहरण: [ ] Is the test Command
#!/bin/bash
if test 5 -eq 5; then
echo "test command: equal"
fi
if [ 5 -eq 5 ]; then
echo "[ ] form: equal"
fi
Login to try C/C++/Java/PHP code in the editor
Numeric Comparisons
[ ] के अंदर, numeric comparisons named operators उपयोग करते हैं: -eq (equal), -ne (not equal), -gt (greater than), -ge (greater or equal), -lt (less than), और -le (less or equal)।
यहां इसके बजाय > या < उपयोग करना shell redirection perform करता है, comparison नहीं, जो एक common mistake है।
उदाहरण: Numeric Comparisons
#!/bin/bash
a=10
b=20
if [ "$a" -lt "$b" ]; then
echo "$a is less than $b"
fi
if [ "$a" -ne "$b" ]; then
echo "$a is not equal to $b"
fi
Login to try C/C++/Java/PHP code in the editor
String Comparisons
[ ] के अंदर, string equality एक single = उपयोग करती है (हालांकि == भी एक Bash extension के रूप में काम करता है) और inequality != उपयोग करती है। String comparisons character-by-character equality जांचते हैं, numeric value नहीं, इसलिए "5" = "05" false होगा भले ही वे same number represent करते हों।
उदाहरण: String Comparisons
#!/bin/bash
name="ada"
if [ "$name" = "ada" ]; then
echo "Name matches"
fi
if [ "$name" != "grace" ]; then
echo "Name is not grace"
fi
Login to try C/C++/Java/PHP code in the editor
File Test Operators
[ ] file test operators भी support करता है जैसे -e (path exists), -f (is a regular file), -d (is a directory), और -r/-w/-x (readable/writable/executable)। ये उन scripts के लिए essential हैं जो filesystem पर act करने से पहले इसे जांचते हैं।
उदाहरण: File Test Operators
#!/bin/bash
touch sample.txt
if [ -e sample.txt ]; then
echo "sample.txt exists"
fi
if [ -f sample.txt ]; then
echo "sample.txt is a regular file"
fi
if [ ! -d sample.txt ]; then
echo "sample.txt is not a directory"
fi
Login to try C/C++/Java/PHP code in the editor
[और]के आसपास required spaces भूल जाना;[$x -eq 5]एक syntax error है, यह[ $x -eq 5 ]दोनों तरफ spaces के साथ होना चाहिए।[ ]के अंदर numeric comparisons के लिए==या>/<उपयोग करना; वहां वे string operators हैं (और>/<को redirection के रूप में treat होने से बचने के लिए escaping चाहिए) -- numeric comparison को-eq,-gt,-lt, आदि चाहिए।[ ]के अंदर variables को quote न करना, जैसे[ $name = "ada" ]; अगर$nameempty है या इसमें spaces हैं, तो यह एक false condition के बजाय एक syntax error में बदल सकता है।
[ ]literallytestcommand है;[ expr ]औरtest exprequivalent हैं,[बस एक alias है जिसे एक matching]चाहिए।- String comparisons
=और!=उपयोग करती हैं; numeric comparisons-eq,-ne,-gt,-ge,-lt,-leउपयोग करती हैं। -f,-d,-eजैसे file tests जांचते हैं कि एक path एक regular file है, एक directory है, या बिलकुल exist करता है।[ ]के अंदर हमेशा variables को quote करें, जैसे[ "$var" = "value" ], errors से बचने के लिए जब variable empty हो या इसमें spaces हों।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: