Ravindra BagaleCourses & study guides English

Shell Scripting Complete 3: Arguments, $@, shift, read आणि getopts ने Script Input

Script input घेऊ शकतो तेव्हा उपयोगी होतो: file नाव, server, दिवसांची संख्या. हा chapter script ला input मिळायचे तीन मार्ग दाखवतो: read ने user कडून, $1, $2, $@ आणि shift ने command line वरून, आणि getopts ने -v किंवा -d DIR सारखे options. Usage message, input check, passwords safely वाचणं आणि pipe किंवा here-document मधून वाचणंही शिकाल.

या chapter मध्ये तुम्ही काय शिकाल

  • read -p, -s आणि -t ने user ला विचारा
  • $1, $#, $@ आणि shift वापरा
  • "$@" vs "$*" समजा
  • getopts ने options parse करा
  • Usage print करा आणि input validate करा
  • stdin, pipes आणि here-docs मधून वाचा

1. रोजचं उदाहरण: चहाच्या टपरीवर order

हे का लागतं?

चहाच्या टपरीवर तीन प्रकारे order देता येते. मालक 'किती?' विचारेपर्यंत थांबून उत्तर देणं (read). सगळं एकदम सांगणं, 'दोन cutting, एक बिनसाखर' (arguments). किंवा menu codes, 'C2 -S' (options). चांगली टपरी तिन्ही समजते आणि तुम्ही चुकीचं बोललात तर menu सांगते.

हे काय आहे?

मालक विचारतो = read. सगळं एकदम = $1 $2. Items ची संख्या = $#. Menu codes = getopts options. Menu दाखवणं = usage message. चुकीची order = exit 2.

कसं करायचं (step by step)

चांगला script input कसा घेतोgetopts ने options parseoptions shift करून काढाRequired arguments साठी $# तपासाप्रत्येक value validateचुकीचं असेल तर usage आणि exit 2खरं काम चालवा
आधी तपासा, मग काम.

2. read: user ला विचारा

हे का लागतं?

काही scripts ना फक्त माणूस देऊ शकेल अशी value लागते, उदा. ticket number किंवा yes/no.

हे काय आहे?

read name line ची वाट बघतो आणि name मधे साठवतो. -p 'text' prompt दाखवतो. -r backslashes जसेच्या तसे ठेवतो (नेहमी वापरा). -s typing लपवतो (passwords). -t 10 10 seconds नंतर सोडून देतो. -n 1 एक key वाचतो. अनेक names दिले तर words प्रत्येकाला आणि उरलेलं शेवटच्याला. नाव नसेल तर line $REPLY मधे.

Optionकाय करतो
-p TEXTprompt दाखवतो
-rraw, backslashes ठेवतो
-ssilent, input लपवतो
-t Nseconds मधे timeout
-n NN characters वाचतो
-a ARRwords array मधे

कसं करायचं (step by step)

  1. नाव विचारा.
read -rp 'Your city: ' city; echo "Hello from $city"

Output:

Your city: Nashik
Hello from Nashik

याचा अर्थ: Type केलेलं city मधे गेलं.

  1. दोन values एकदम.
read -rp 'First Last: ' first last; echo "$last, $first"

Output:

First Last: Sachin Tendulkar
Tendulkar, Sachin

याचा अर्थ: Words spaces वर तुटले.

  1. लपवलेला password.
read -rsp 'DB password: ' pw; echo; echo "got ${#pw} chars"

Output:

DB password: 
got 9 chars

याचा अर्थ: Typing करताना काहीच दिसलं नाही.

  1. Timeout.
read -t 5 -rp 'Continue? ' ans || echo ' no answer, using default'

Output:

Continue?  no answer, using default

याचा अर्थ: Timeout वर read non-zero देतो.

3. धोकादायक कामाआधी Yes/no confirmation

हे का लागतं?

Delete, restart किंवा deploy आधी विचारायला हवं, म्हणजे बोट घसरून outage होत नाही.

हे काय आहे?

एक उत्तर वाचा, lower-case करा, आणि फक्त y किंवा yes वरच पुढे जा. बाकी सगळं (Enter सुद्धा) no. Automation ला प्रश्न टाळता यावा म्हणून -y option द्या (खाली शिकवलं).

कसं करायचं (step by step)

  1. Confirm.
read -rp 'Restart nginx? [y/N] ' a; [[ ${a,,} == y* ]] && echo restarting || echo cancelled

Output:

Restart nginx? [y/N] N
cancelled

याचा अर्थ: Default No.

  1. एक key.
read -n1 -rp 'Press any key to go on...'; echo

Output:

Press any key to go on...

याचा अर्थ: एका key ची वाट.

  1. Terminal नाही?
[ -t 0 ] && echo interactive || echo 'no tty, skip questions'

Output:

interactive

याचा अर्थ: Cron मधे [ -t 0 ] false, म्हणून विचारू नका.

4. Positional arguments: $1, $2, $#, $0

हे का लागतं?

Arguments मुळे इतर लोक आणि cron प्रश्नांशिवाय script चालवू शकतात: ./deploy.sh shop prod.

हे काय आहे?

Script नावानंतरचे words $1, $2 ... ${10} होतात (10 पासून braces). $# म्हणजे किती. $0 script नाव. Spaces असलेले arguments caller ने quote करायला हवेत: ./s.sh "my file". आधी $# तपासा आणि काही नसेल तर usage message सह थांबा.

कसं करायचं (step by step)

  1. बघा.
printf '#!/bin/bash\necho "script=$0 count=$# first=$1 second=$2"\n' | tee args.sh && bash args.sh shop prod

Output:

script=args.sh count=2 first=shop second=prod

याचा अर्थ: प्रत्येक word ला number.

  1. Quoted argument.
bash args.sh "Pune Camp" east

Output:

script=args.sh count=2 first=Pune Camp second=east

याचा अर्थ: Quotes नी 'Pune Camp' एक ठेवलं.

  1. Usage check.
bash -c '[ $# -ge 1 ] || { echo "usage: $0 FILE"; exit 2; }; echo ok' myscript

Output:

usage: myscript FILE

याचा अर्थ: bash -c मधे code नंतरचा पहिला word $0 होतो.

  1. दहावा argument.
set -- a b c d e f g h i j; echo "${10}"

Output:

j

याचा अर्थ: set -- arguments set करतो; ${10} ला braces.

5. "$@" vs "$*" आणि arguments पुढे देणं

हे का लागतं?

योग्य प्रकार न वापरल्यास wrappers आणि arguments वरचे loops spaces वर बिघडतात.

हे काय आहे?

"$@" प्रत्येक argument वेगळा, quoted word म्हणून देतो. 99% वेळा हेच हवं. "$*" सगळे arguments एका string मधे space ने जोडतो (IFS चा पहिला char). Unquoted $@ आणि $* परत spaces वर तुटतात, म्हणून टाळा. सगळे arguments दुसऱ्या command ला: other_cmd "$@".

प्रकारArgs: 'a b' cकाय देतो
"$@"2 words[a b] [c]
"$*"1 word[a b c]
$@3 words[a] [b] [c]

कसं करायचं (step by step)

  1. Safely loop.
set -- 'a b' c; for x in "$@"; do echo "[$x]"; done

Output:

[a b]
[c]

याचा अर्थ: प्रत्येक argument अख्खा.

  1. जोडलेलं.
set -- 'a b' c; echo "[$*]"

Output:

[a b c]

याचा अर्थ: एक string.

  1. Wrapper.
printf '#!/bin/bash\necho "[wrapper] running ls $*"\nexec ls -l "$@"\n' | tee myls.sh && bash myls.sh /etc/hosts

Output:

[wrapper] running ls /etc/hosts
-rw-r--r-- 1 root root 220 Oct 1 /etc/hosts

याचा अर्थ: Message साठी $*, command साठी "$@".

6. shift: arguments मधून पुढे जा

हे का लागतं?

पहिले arguments special असतात (उदा. command नाव) तेव्हा ते काढून उरलेले list म्हणून हवे.

हे काय आहे?

shift $1 काढतो आणि प्रत्येक argument एक जागा डावीकडे हलवतो, म्हणजे $2 चा $1 होतो आणि $# एकने कमी. shift 2 दोन काढतो. नेहमीचा loop: while [ $# -gt 0 ]; do ...; shift; done. getopts ला न जमणाऱ्या --dry-run सारख्या simple long options साठी वापरा.

कसं करायचं (step by step)

  1. Basic.
set -- deploy web1 web2; cmd=$1; shift; echo "$cmd on: $*"

Output:

deploy on: web1 web2

याचा अर्थ: Command list मधून काढली.

  1. Loop.
set -- x y z; while [ $# -gt 0 ]; do echo "item $1 (left $#)"; shift; done

Output:

item x (left 3)
item y (left 2)
item z (left 1)

याचा अर्थ: प्रत्येक shift पुढच्याकडे.

  1. Hand ने long options.
set -- --dry-run --env stage app; while [ $# -gt 0 ]; do case $1 in --dry-run) dry=1;; --env) env=$2; shift;; *) break;; esac; shift; done; echo "dry=$dry env=$env rest=$*"

Output:

dry=1 env=stage rest=app

याचा अर्थ: --env value घेतो, म्हणून दोनदा shift.

7. getopts: -v आणि -d DIR सारखे नीट options

हे का लागतं?

Users ना tools -h, -v, -n 5 कोणत्याही क्रमाने आणि -vn 5 असे एकत्र स्वीकारावेत असं वाटतं. getopts हे तुमच्यासाठी करतो.

हे काय आहे?

while getopts 'hvd:' opt; do case $opt in ... esac; done. प्रत्येक letter एक option; नंतर colon (d:) म्हणजे value लागते, जी $OPTARG मधे. सुरुवातीचा colon (':hvd:') errors तुम्हीच handle करू देतो: अनोळखी option साठी \? आणि value नसेल तर :. Loop नंतर shift $((OPTIND-1)) options काढतो म्हणजे $1 पहिला साधा argument. getopts फक्त short options handle करतो.

भागअर्थ
'hvd:'h, v आणि value सह d
$optसापडलेलं letter
$OPTARGd ची value
$OPTINDपुढच्या argument चा index
leading :तुम्ही handle करायचे silent errors

कसं करायचं (step by step)

  1. Script.
cat backup.sh

Output:

#!/bin/bash
verbose=0; dest=/backup
while getopts ':hvd:' opt; do
  case $opt in
    h) echo "usage: $0 [-v] [-d DIR] SRC..."; exit 0;;
    v) verbose=1;;
    d) dest=$OPTARG;;
    :) echo "-$OPTARG needs a value"; exit 2;;
    \?) echo "unknown -$OPTARG"; exit 2;;
  esac
done
shift $((OPTIND-1))
echo "verbose=$verbose dest=$dest sources=$*"

याचा अर्थ: h आणि v flags; d ला value.

  1. वापरा.
./backup.sh -v -d /mnt/usb /etc /home

Output:

verbose=1 dest=/mnt/usb sources=/etc /home

याचा अर्थ: shift नंतर फक्त folders राहिले.

  1. एकत्र flags.
./backup.sh -vd /srv/bk /var/www

Output:

verbose=1 dest=/srv/bk sources=/var/www

याचा अर्थ: -vd /srv/bk म्हणजे -v -d /srv/bk.

  1. Errors.
./backup.sh -x; ./backup.sh -d

Output:

unknown -x
-d needs a value

याचा अर्थ: स्पष्ट messages आणि exit 2.

8. Usage message आणि input checks

हे का लागतं?

बहुतेक चुकीचे runs चुकीच्या input मुळे. वर तपासा आणि script कसा वापरायचा ते सांगा.

हे काय आहे?

usage() function लिहा जो syntax stderr (>&2) ला print करतो आणि exit 2. $# चुकीचा असेल किंवा -h दिला तर call करा. Numbers numbers आहेत ([[ $n =~ ^[0-9]+$ ]]), files आहेत ([ -f ]), folders writable ([ -w ]) ते तपासा. if chapter मधे हे tests.

कसं करायचं (step by step)

  1. Usage function.
usage(){ echo "usage: cleanup.sh DAYS DIR" >&2; exit 2; }; type usage | head -1

Output:

usage is a function

याचा अर्थ: stderr ला print करून exit.

  1. Number check.
n=abc; [[ $n =~ ^[0-9]+$ ]] && echo number || echo 'not a number'

Output:

not a number

याचा अर्थ: Regex फक्त digits.

  1. File check.
f=/etc/nope.conf; [ -f "$f" ] || echo "missing $f" >&2

Output:

missing /etc/nope.conf

याचा अर्थ: काही करण्याआधी थांबा.

9. stdin, pipes आणि files मधून वाचणं

हे का लागतं?

चांगले tools pipes मधे चालतात: cat list.txt | ./check.sh. तुमचा script त्याच प्रकारे lines वाचू शकतो.

हे काय आहे?

File दिली नसेल तर standard input मधून वाचा. while IFS= read -r line; do ...; done एका वेळी एक line वाचतो (chapter 5 मधे IFS). < file file आत पाठवतो. Here-string <<< "text" एक string पाठवतो. Here-document <

कसं करायचं (step by step)

  1. Pipe ने.
printf 'web1\nweb2\n' | while IFS= read -r h; do echo "checking $h"; done

Output:

checking web1
checking web2

याचा अर्थ: प्रत्येक line क्रमाने.

  1. Here-string.
read -r a b <<< 'Pune 411001'; echo "$b $a"

Output:

411001 Pune

याचा अर्थ: एक string read मधे.

  1. Here-document.
cat <<EOF
Host: $(hostname)
User: $USER
EOF

Output:

Host: web01
User: asha

याचा अर्थ: आत variables expand झाले.

  1. File किंवा stdin.
in=${1:-/dev/stdin}; wc -l < "$in"

Output:

3

याचा अर्थ: File नसेल तर /dev/stdin.

10. अनेक industries मधली खरी उदाहरणं

हे का लागतं?

तेच Linux skill प्रत्येक प्रकारच्या company मध्ये लागतं. वेगवेगळ्या ठिकाणी पाहिलं की लक्षात राहतं.

हे काय आहे?

प्रत्येक row म्हणजे एक काल्पनिक team. कोण हे वापरतं आणि कसं ते दाखवते.

कसं करायचं (step by step)

Team आणि शहरते कसं वापरतातCommand
Kiran, school ERP, Kolhapurread -p ने class नावread -rp 'Class: ' cls
Emma, retail, DublinReport tool मधे getoptswhile getopts 'd:o:' o
Wei, fintech, ShanghaiSilent password promptread -rsp 'Pass: ' p
Omar, airline, Doha"$@" देणारा wrapperexec ansible "$@"
Julia, media, BerlinPipe मधून batchcut -d, -f1 ids.csv | ./fetch.sh
Vikram, logistics, ChennaiUsage आणि exit 2usage(){ ...; exit 2; }

11. नेहमीच्या errors आणि त्यांचे fixes

हे का लागतं?

हे errors सगळ्यांना येतात. कारण माहीत असलं की तास वाचतात.

हे काय आहे?

आधी नेमका message वाचा. मग खालच्या row शी जुळवा.

कसं करायचं (step by step)

दिसणारा errorका होतंFix कसा करायचा
read hangs in cronType करायला terminal नाहीArguments किंवा [ -t 0 ].
[ -t 0 ] || exit 1
Backslashes vanished from input-r शिवाय readread -r वापरा.
read -r line
$10 printed as first argument plus 0$10 म्हणजे $1 आणि 0Braces वापरा.
echo "${10}"
File with spaces split into two argumentsUnquoted $@ किंवा $*"$@" वापरा.
for a in "$@"
illegal option -- xgetopts ला अनोळखी optionAdd करा किंवा \? handle.
getopts ':ab:' o
option requires an argument -- d-d value शिवायValue द्या किंवा : handle.
./s.sh -d /tmp
Arguments still include -v after getoptsshift $((OPTIND-1)) विसरलातOptions shift करा.
shift $((OPTIND-1))
getopts ignores --longgetopts फक्त short optionswhile/case loop किंवा getopt.
case $1 in --env) ...;; esac
shift count out of rangeArguments नसताना shiftshift आधी $#.
[ $# -gt 0 ] && shift

12. Troubleshooting step by step

हे का लागतं?

Script ला खरं काय मिळालं ते print करा.

हे काय आहे?

वरून खाली जा. ज्या step ला problem दिसतो तिथे थांबा.

कसं करायचं (step by step)

  1. Count.
echo "count=$#"
  1. प्रत्येक argument.
printf '[%s]\n' "$@"
  1. Option state.
echo "OPTIND=$OPTIND OPTARG=$OPTARG"
  1. Terminal?
[ -t 0 ] && echo tty
  1. Parsing trace.
bash -x s.sh -v -d /tmp x
  1. Input मधले hidden characters.
printf '%q\n' "$1"

13. Lab 1: read आणि arguments सह greeting script

हे का लागतं?

greet.sh लिहा जो $1 दिला असेल तर वापरतो, नाहीतर read ने विचारतो, आणि input तपासतो.

हे काय आहे?

पूर्ण hands-on lab. प्रत्येक step test Linux machine वर करा (EC2 instance, VM किंवा WSL).

कसं करायचं (step by step)

  1. Folder.
mkdir -p ~/lab39 && cd ~/lab39
  1. लिहा.
printf '#!/bin/bash\nname=${1:-}\nif [ -z "$name" ]; then read -rp "Name: " name; fi\n[ -n "$name" ] || { echo "name required" >&2; exit 2; }\necho "Namaskar, $name!"\n' | tee greet.sh && chmod +x greet.sh
  1. Argument सह.
./greet.sh Meera

Output:

Namaskar, Meera!
  1. शिवाय.
./greet.sh

Output:

Name: Raj
Namaskar, Raj!
  1. रिकामं input.
./greet.sh <<< ''; echo $?

Output:

name required
2
  1. Pipe मधून.
echo Sara | ./greet.sh

Output:

Namaskar, Sara!

14. Lab 2: खरं getopts tool

हे का लागतं?

-n COUNT, -d DIR आणि -h सह findbig.sh बनवा जो सगळ्यात मोठ्या files दाखवतो.

हे काय आहे?

पूर्ण hands-on lab. प्रत्येक step test Linux machine वर करा (EC2 instance, VM किंवा WSL).

कसं करायचं (step by step)

  1. Parser लिहा.
printf '#!/bin/bash\nn=5; d=.\nwhile getopts ":hn:d:" o; do case $o in h) echo "usage: $0 [-n N] [-d DIR]"; exit 0;; n) n=$OPTARG;; d) d=$OPTARG;; :) echo "-$OPTARG needs value" >&2; exit 2;; \\?) echo "bad -$OPTARG" >&2; exit 2;; esac; done\nshift $((OPTIND-1))\n[[ $n =~ ^[0-9]+$ ]] || { echo "N must be a number" >&2; exit 2; }\ndu -ah "$d" 2>/dev/null | sort -rh | head -n "$n"\n' | tee findbig.sh && chmod +x findbig.sh
  1. Help.
./findbig.sh -h

Output:

usage: ./findbig.sh [-n N] [-d DIR]
  1. Default.
./findbig.sh -d /var/log | head -2

Output:

1.2G /var/log
800M /var/log/journal
  1. /etc मधे तीन.
./findbig.sh -n 3 -d /etc

Output:

6.1M /etc
3.0M /etc/selinux
1.1M /etc/ssl
  1. चुकीचा number.
./findbig.sh -n many; echo $?

Output:

N must be a number
2
  1. Value नाही.
./findbig.sh -d

Output:

-d needs value

Ravindra Bagale's Tip

Always quote "$@" when you pass arguments on, and always end a getopts loop with shift $((OPTIND-1)). These two lines are where most argument bugs hide.

15. नेहमी विचारले जाणारे प्रश्न

प्र. read की arguments?

Automation साठी arguments, माणसांसाठी read.

प्र. read -r का?

Backslashes ठेवतो.

प्र. getopts ने long options?

Support नाही; case loop किंवा GNU getopt.

प्र. OPTIND काय?

पुढच्या argument चा index.

प्र. चुकीच्या usage ला exit 2 का?

Misuse साठी convention.

प्र. Usage कुठे?

stderr ला >&2.

प्र. File मधून password?

हो; chmod 600 सह read -r pw < file.

प्र. $REPLY काय?

read चा default variable.

16. मुलाखत प्रश्न आणि उत्तरं

Beginner level

प्र. पहिला argument?

$1.

प्र. Arguments ची संख्या?

$#.

प्र. User ला prompt?

read -p.

प्र. Password लपवायचा?

read -s.

Mid level level

प्र. "$@" vs "$*"?

वेगळे words vs एक string.

प्र. shift काय करतो?

$1 काढून बाकी हलवतो.

प्र. -d ची getopts value?

Spec मधे d:, value $OPTARG मधे.

प्र. shift $((OPTIND-1)) का?

फक्त non-option args ठेवायला.

Senior level

प्र. Deploy script चा CLI?

Flags ला getopts, targets positional, usage, validation, prompts टाळायला -y.

प्र. Pipes आणि files दोन्ही?

"${1:-/dev/stdin}" मधून वाचा.

प्र. Silent vs normal getopts errors?

सुरुवातीचा colon: तुम्ही \? आणि : handle.

प्र. Secrets safely?

Env var किंवा file, argument नाही (ps मधे दिसतं).

घरी करून बघा

  1. add.sh लिहा जो arguments म्हणून दिलेले सगळे numbers बेरीज करून total दाखवतो. Number नसलेलं नाकारा.
  2. त्याला getopts ने -q (quiet) आणि -h (help) options द्या.

सराव Tasks

या scenario बद्दल

London Legal LLP हा लंडन मधला कायदा सेवा व्यवसाय आहे. Oliver Hughes त्यांचं IT व cloud काम पाहतात. त्यांचे पैसे GBP मध्ये मोजले जातात आणि AWS resources eu-west-2 Region मध्ये चालतात. खालच्या tasks मध्ये तुम्ही या lesson मध्ये शिकलेलं वापरून त्यांची खरी कामं करता. सगळी नावं, account IDs, IP addresses व numbers सरावासाठी काल्पनिक आहेत.

0 / 21

सोपं नाव विचारा

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: "Branch: " विचारा आणि उत्तर br मधे, backslashes ठेवून.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

सोपं किती

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: Script ला किती arguments मिळाले ते print.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

सोपं पहिला argument

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: पहिला argument quotes मधे print.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

सोपं Script नाव

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: Script कोणत्या नावाने call झाला ते print.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

सोपं तीन काढा

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: पहिले तीन arguments काढा.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

सोपं Option value

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: Current option value outdir मधे.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम लपवलेला PIN

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: "PIN: " prompt सह लपवलेली value pin मधे.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम Timeout

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: 15 second timeout सह, raw mode मधे ans मधे read.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम एक key

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: एकच character key मधे वाचा.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम अकरावा

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: अकरावा argument print.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम प्रत्येक argument line वर

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: प्रत्येक argument वेगळ्या line वर printf ने, spaces ठेवून.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम Arguments set

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: Arguments alpha beta gamma set करा.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम getopts spec

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: q आणि f flags साठी getopts loop, f ला value, letter o मधे.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम Options काढा

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: getopts loop नंतर parse केलेले सगळे options काढा.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम Output terminal ला?

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: Standard output terminal आहे का तपासा आणि असेल तर term print.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम Here-string

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: "Satara 415001" here-string ने town आणि code मधे read.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम पुढे द्या

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: Script चे सगळे arguments safely देऊन rsync चालवा.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

अवघड Words array मधे

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: एक line words array मधे, raw mode.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

अवघड Silent getopts

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: h आणि value सह n साठी silent-error getopts loop, letter opt मधे.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

अवघड File किंवा stdin

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: src मधे पहिला argument, नसेल तर /dev/stdin.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

अवघड Number आहे

का: BIZ च्या tools ना खऱ्या commands सारखे options लागतात.

काय करायचं: qty variable फक्त digits आहे हे [[ ]] ने तपासा आणि ok print.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

थोडक्यात सांगायचं तर

  1. read -rp विचारतो; -s लपवतो; -t timeout.
  2. $1..$9, ${10}, $# आणि $0 मधे arguments.
  3. "$@" ने arguments पुढे द्या.
  4. shift arguments मधून पुढे.
  5. getopts 'hvd:' $OPTARG सह, मग shift $((OPTIND-1)).
  6. Input validate करा आणि usage stderr ला exit 2 सह.

उजळणी, तुलना आणि मुलाखत cards

हा lesson काही मिनिटांत revise करा. Cards phone वर save करा किंवा PNG download करा. प्रत्येक card वरचा QR हा lesson उघडतो.

Script arguments. $1, $2 = पहिला, दुसरा. $# = किती. "$@" = सगळे, safe. पहिला काढा.
उजळणी card: Script arguments · PNG download करा · Card page उघडा
4 steps मधे getopts. Flags; colon = value लागते. while loop. Letter वर case. Value OPTARG मधे.
उजळणी card: 4 steps मधे getopts · PNG download करा · Card page उघडा
arguments vs read. Command line वर. / चालताना type.
तुलना: arguments vs read · PNG download करा · Card page उघडा
getopts vs while/case. Built in, short flags. / हाताने लिहिलेला loop.
तुलना: getopts vs while/case · PNG download करा · Card page उघडा
"$@" vs "$*"?. "$@" प्रत्येक argument वेगळा. "$*" एकत्र जोडतो. जवळजवळ नेहमी "$@".
मुलाखत card: "$@" vs "$*"? · PNG download करा · Card page उघडा
Arguments missing?. आधी $# तपासा. stderr वर usage. Code 2 ने exit.
मुलाखत card: Arguments missing? · PNG download करा · Card page उघडा
Password safe वाचायचा?. read -s typing लपवतो. -p prompt दाखवतो. उदाहरण.
मुलाखत card: Password safe वाचायचा? · PNG download करा · Card page उघडा
getopts vs manual parsing?. getopts: short flags, built in. Long --flags साठी manual case loop. getopts नंतर shift.
मुलाखत card: getopts vs manual parsing? · PNG download करा · Card page उघडा

अजून cards: Ravi Sir's Cards.