test and [ ] command for Condition Testing (file, numeric, string)

एक if statement में square brackets असल में test command हैं, और यह तीन kinds की conditions check करता है: एक file exist करती है या इसका एक type है, दो numbers कैसे compare होते हैं, और दो strings match करती हैं या नहीं।

11 min read · 7 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

उन Brackets के अंदर क्या है?

आपने if [ $n -lt 5 ] लिखा है बिना really पूछे [ ] क्या है। यहाँ surprise है: square brackets एक command हैं, जिसे test कहते हैं। [ $n -lt 5 ], test command चलाता है, जो condition check करता है और success या failure return करता है, exactly वह exit status जिस पर if और && rely करते हैं।

तो conditions master करने का मतलब है test master करना। यह तीन kinds की चीज़ें check करता है: files, numbers, और strings। यह lesson तीनों बताता है, और वे syntax rules जो beginners को पकड़ते हैं।

At a glance

KindOperatorsExample
File-e exists, -f regular file, -d directory, -r/-w/-x readable/writable/executable[ -f notes.txt ]
Numeric-eq equal, -ne not equal, -lt, -le, -gt, -ge[ $n -gt 3 ]
String= equal, != not equal, -z empty, -n non-empty[ "$a" = "$b" ]

Practical

Files, Numbers, और Strings Testing (verified)

$ [ -f real.txt ] && echo "it is a regular file"
it is a regular file
$ [ -d demo ] && echo "demo is a directory"
demo is a directory
$ [ 5 -gt 3 ] && echo "5 is greater than 3"
5 is greater than 3
$ [ "abc" = "abc" ] && echo "strings equal"
strings equal
$ [ -z "" ] && echo "empty string"
empty string

Watch out

Numbers -eq इस्तेमाल करते हैं, Strings = , और Spaces का ध्यान रखिए

दो rules endless grief बचाते हैं। पहला, type के लिए सही operators इस्तेमाल कीजिए: NUMBERS को -eq -ne -lt -le -gt -ge से compare कीजिए, और STRINGS को = != -z -n से। [ $n = 5 ] लिखना strings की तरह compare करता है, और text पर [ "$a" -eq "$b" ] एक error है। Data से operator match कीजिए।

दूसरा, brackets के अंदर और operator के around spaces mandatory हैं: [ $n -lt 5 ] काम करता है, पर [$n -lt 5] या [ $n -lt5 ] fail होता है, क्योंकि [ एक command है और इसके arguments separated चाहिए। ये दो mistakes ज़्यादातर broken shell conditions के लिए ज़िम्मेदार हैं।

Quiz

आप check करना चाहते हैं variable n में number 10 के equal है या नहीं। कौन सा test correct है?

  1. [ $n = 10 ], comparison के लिए = इस्तेमाल करते हुए
  2. [ $n -eq 10 ], numeric-equality operator -eq इस्तेमाल करते हुए
  3. [$n -eq 10], बिना spaces के tightly packed
  4. [ $n == equal 10 ]
Show the answer

[ $n -eq 10 ], numeric-equality operator -eq इस्तेमाल करते हुए

Numbers numeric operators से compare होते हैं, और equality -eq है, तो [ $n -eq 10 ] correct है। Option A = इस्तेमाल करता है, जो STRING equality operator है; यह simple cases के लिए काम करता दिख सकता है पर numeric comparison के लिए ग़लत tool है और differently behave करता है (उदाहरण के लिए, यह '10' और '010' को अलग strings treat करेगा भले ही ये numerically equal हों)। Option C required SPACES omit करता है: [ एक command है और इसे अपने arguments separated चाहिए, तो [$n को [ $n होना चाहिए। Option D invented syntax है। याद रखिए: numbers के लिए -eq, strings के लिए =, और हमेशा brackets के अंदर spaces रखिए।

Think first

Numeric और String Equality अलग Operators क्यों हैं?

Shell numbers के लिए -eq और strings के लिए = पर insist क्यों करता है, सब कुछ के लिए एक equals की बजाय? फिर tap कीजिए।

Show the answer

क्योंकि NUMBERS की तरह compare करना और TEXT की तरह compare करना अलग answers दे सकते हैं, तो shell आपसे कहलवाता है आपका मतलब कौन सा है। Shell में सब कुछ text की तरह life शुरू करता है, और text की तरह, '10' और '010' और ' 10 ' तीन DIFFERENT strings हैं, पर numbers की तरह ये सब ten के equal हैं। अगर आप -eq इस्तेमाल करें, shell दोनों sides को integers की तरह interpret करता है और इनकी VALUES compare करता है, तो [ 10 -eq 010 ] true है। अगर आप = इस्तेमाल करें, यह raw CHARACTERS compare करता है, तो [ 10 = 010 ] false है क्योंकि strings अलग हैं। कोई भी generally 'right' नहीं है; यह depend करता है आप numeric value की care करते हैं या exact text की। अलग operators provide करके, shell आपको explicit होने देता है और silently wrong guess करने से बचता है। यह real scripts में matter करता है: एक user के typed input, एक version number, या एक count को compare करना differently behave करता है depending on आप कौन सा choose करते हैं। Discipline simple है और जल्दी build करने लायक है: अगर चीज़ एक number है और आप इसकी value की care करते हैं, -eq और इसके numeric siblings इस्तेमाल कीजिए; अगर यह text है और आप exact characters की care करते हैं, = और != इस्तेमाल कीजिए। सही comparison चुनना subtle, hard-to-spot bugs prevent करता है।

Summary

Key takeaways

  • एक if statement में [ ] असल में test command है, जो एक condition के लिए success या failure return करता है।
  • File tests: -e exists, -f regular file, -d directory, -r/-w/-x readable/writable/executable।
  • Numeric comparison: -eq, -ne, -lt, -le, -gt, -ge।
  • String comparison: = equal, != not equal, -z empty (zero-length), -n non-empty।
  • Numbers के लिए numeric operators और strings के लिए string operators इस्तेमाल कीजिए; इन्हें mix करना wrong results या errors देता है।
  • Brackets के अंदर और operators के around spaces mandatory हैं: [ $n -lt 5 ], [$n -lt5 नहीं।
  • Memory hook: test files check करता है (-f/-d), numbers (-eq/-lt), strings (=); spaces required।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Shell Scripting in Linux

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati