Creating and Executing Shell Scripts (nano, vi, ./script.sh)

एक shell script बस commands से भरी एक text file है: आप इसे nano या vi जैसे editor में लिखते हैं, इसे shell का नाम देने वाली एक shebang line से शुरू करते हैं, इसे chmod +x से executable बनाते हैं, और इसे ./script.sh से run करते हैं।

10 min read · 7 cards · 2 checks

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


Theory

Commands को एक Program में Bundle करना

अब तक आपने commands एक-एक करके type किए हैं। पर backup task, एक folder बनाओ, files copy करो, इन्हें timestamp करो, कई commands है जो आप बार-बार चलाएँगे। हर बार इन्हें type करना tedious और error-prone है।

Jawab है एक shell script: उन commands वाली एक text file, जिसे आप एक single program की तरह run करते हैं। यहीं से Linux commands के एक set से एक programming environment में बदल जाता है। यह lesson cover करता है एक script कैसे create करें, वह एक line जो top पर जाती है, और वह step जो beginners सबसे ज़्यादा भूलते हैं: इसे executable बनाना।

Theory

एक Script लिखना: Editor और Shebang

आप terminal में एक text editor में एक script create करते हैं। nano beginners के लिए friendly choice है (on-screen hints, save और quit करना आसान); vi (या vim) ज़्यादा powerful है और हर system पर मिलता है, पर modal और सीखने में trickier है।

एक script की पहली line special होती है: वह shebang, #!/bin/bash लिखी हुई। यह system को बताती है कौन सा interpreter file चलाए, यहाँ, Bash। इसके बाद, आप ordinary commands लिखते हैं, एक per line, exactly जैसे आप इन्हें type करते। File को convention से एक .sh नाम से save कीजिए, और आपके पास एक script है।

Practical

एक पहली Script, और इसे Run करना (verified)

# In nano, create backup.sh containing:
#!/bin/bash
echo "Backing up..."

# Back at the shell, a new file is NOT executable yet:
$ ls -l backup.sh
-rw-r--r-- 1 riya riya ... backup.sh      # no x bits!

$ chmod +x backup.sh          # add execute permission
$ ls -l backup.sh
-rwxr-xr-x 1 riya riya ... backup.sh      # now executable

$ ./backup.sh                 # run it (note the ./)
Backing up...

Watch out

Classic Trap: chmod +x, और ./

एक brand-new script execute permission के बिना create होती है (rw-r--r--)। अगर आप इसे run करने की कोशिश करें, आपको 'Permission denied' मिलता है। Fix है `chmod +x script.sh`, जो execute bit add करता है (permissions वाले lesson से directly link करते हुए)।

यह भी नोट कीजिए आप इसे `./script.sh` की तरह run करते हैं, leading ./ के साथ। यह कहता है 'यह script जो ठीक इसी directory में है'। इसके बिना, shell सिर्फ़ अपने PATH पर देखता है और आपकी script नहीं ढूँढता। दो छोटे steps, chmod +x, फिर ./, लगभग हर beginner को कम से कम एक बार trip करते हैं।

Quiz

आपने अभी-अभी nano में backup.sh create की और ./backup.sh type किया, पर 'Permission denied' मिला। Fix क्या है?

  1. File का नाम बदलकर backup.exe कीजिए
  2. chmod +x backup.sh चलाइए execute permission add करने के लिए, फिर ./backup.sh फिर से
  3. Server reboot कीजिए
  4. Script में और commands add कीजिए
Show the answer

chmod +x backup.sh चलाइए execute permission add करने के लिए, फिर ./backup.sh फिर से

एक नई script execute bit के बिना create होती है (permissions rw-r--r--), तो इसे run करने की कोशिश 'Permission denied' देती है। Fix है chmod +x backup.sh, जो execute permission add करता है (इसे rwxr-xr-x में बदलते हुए), जिसके बाद ./backup.sh चलता है। Option A ग़लत है: Linux .exe extensions इस्तेमाल नहीं करता; execute permission, नाम नहीं, runnability decide करता है। Option C nonsense है: एक reboot file permissions नहीं बदलता। Option D point miss करता है: permission error से script के contents irrelevant हैं; यह बस अभी तक executable mark नहीं है। एक नई script के लिए दो-step ritual याद रखिए: chmod +x एक बार, फिर ./ से run।

Think first

आपको सिर्फ़ script.sh की बजाय ./script.sh क्यों Type करना पड़ता है?

ls जैसी commands बिना किसी ./ के front में चलती हैं। आपकी अपनी script को ./ की ज़रूरत क्यों पड़ती है? फिर tap कीजिए।

Show the answer

क्योंकि shell commands कैसे FIND करता है इसकी वजह से, directories की एक list इस्तेमाल करते हुए जिसे PATH कहते हैं, और आपकी current directory normally इस पर NAHI होती। जब आप ls type करते हैं, shell PATH में directories search करता है (जैसे /bin और /usr/bin), वहाँ ls ढूँढता है, और इसे run करता है। आपकी नई script आपकी current directory में बैठती है, जिसे security reasons के लिए deliberately PATH से बाहर रखा जाता है, अगर current directory automatically search होती, एक attacker एक common command जैसे नाम वाली एक malicious file एक folder में drop कर सकता था और आपसे इसे accidentally run करा सकता था। तो current directory में एक script run करने के लिए, आपको shell को explicitly बताना पड़ता है यह कहाँ है, और ./ का मतलब है 'ठीक इसी directory में'। (याद कीजिए . current directory है)। ./backup.sh इस लिए कहता है 'यहाँ मौजूद backup.sh चलाओ', PATH search bypass करते हुए। अगर आप इसे बिना ./ के चलाना चाहते, आप या तो इसे एक PATH directory में move करते (जैसे /usr/local/bin) या इसकी directory PATH में add करते। ./ एक छोटा safety feature है, कोई nuisance नहीं: यह shell को सिर्फ़ इसलिए files silently run करने से रोकता है क्योंकि आप संयोग से उनके folder में खड़े हैं।

Summary

Key takeaways

  • एक shell script commands की एक text file है जो एक single program की तरह run होती है, backups जैसे repeated tasks के लिए ideal।
  • Scripts एक editor में लिखिए: nano (beginner-friendly) या vi/vim (powerful, हर system पर)।
  • पहली line shebang है, #!/bin/bash, वह interpreter नाम देते हुए जो file चलाए।
  • एक नई script execute permission के बिना create होती है, तो पहले आपको chmod +x script.sh चलाना पड़ता है।
  • Current directory में एक script ./script.sh की तरह run कीजिए; leading ./ कहता है 'ठीक यहाँ'।
  • आपको ./ चाहिए क्योंकि current directory PATH पर नहीं है (एक security choice), तो shell script otherwise नहीं ढूँढेगा।
  • Memory hook: top पर shebang, एक बार chmod +x, ./ से run।

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

Creating and Executing Shell Scripts (nano, vi, ./script.sh) · Linux Operating System (LOS) (Minor-04) · Gri-Learn