Ravindra BagaleCourses & study guides English

Shell Scripting Complete 6: Bash Functions, local, return आणि Reusable Script Libraries

Functions commands च्या block ला नाव देतात म्हणजे तो reuse, test करता येतो आणि script गोष्टीसारखा वाचता येतो: check_disk, backup_db, send_alert. हा chapter functions define आणि call करणं, functions मधले arguments, return codes vs output, local variables, echo आणि nameref ने values परत देणं, आणि अनेक scripts source करतात अशी shared library file बनवणं शिकवतो. main function, cleanup साठी traps आणि सोपी logging library पण शिकाल.

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

  • Functions define आणि call करा
  • Functions मधे $1, $@ वापरा
  • return vs echo समजा
  • Variables local ठेवा
  • Library बनवा आणि source करा
  • स्वच्छ scripts साठी main आणि trap

1. रोजचं उदाहरण: pressure cooker recipe card

हे का लागतं?

पुण्याच्या प्रत्येक घरात काही ठरलेल्या steps असतात: 'फोडणी देणं' वरण, भाजी आणि पोह्यासाठी सारखंच. आई दर वेळी समजावत नाही; फक्त 'फोडणी कर' म्हणते. Function म्हणजे ती नावाची step. सगळ्या कुटुंबाने share केलेली recipe file म्हणजे library, आणि प्रत्येक recipe card फक्त 'फोडणी step वापर' म्हणू शकतं.

हे काय आहे?

नावाची step = function. देता ते साहित्य = arguments $1 $2. 'छान झालं' की 'करपलं' = return code. शिजलेला पदार्थ = echo चा output. तुमची स्वतःची वाटी = local variable. कुटुंबाची recipe file = source करायची library.

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

Function call कसा चालतोBash नावाने function शोधतोArguments आत $1, $2 होतातlocal variables बनतातBody चालतेreturn exit code set करतो (किंवा शेवटची command)Caller कडे परत; $? मधे code
Function call करण्याआधी define करा.

2. Function define आणि call करा

हे का लागतं?

Repeated blocks मुळे scripts लांब आणि bugs पण repeat. एक function म्हणजे fix करायची एक जागा.

हे काय आहे?

name() { commands; } लिहा आणि command सारखं call करा: name. function name { } प्रकारही bash मधे चालतो. नावात letters, digits आणि _. Call करणारी line चालण्याआधी function define हवा. declare -f name code दाखवतो; type name function आहे हे सांगतो.

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

  1. Define आणि call.
greet() { echo "Namaskar from $(hostname)"; }; greet

Output:

Namaskar from web01

याचा अर्थ: कोणत्याही command सारखं call.

  1. बघा.
type greet

Output:

greet is a function
greet () 
{ 
    echo "Namaskar from $(hostname)"
}

याचा अर्थ: Bash ने code साठवला.

  1. खूप लवकर call.
bash -c 'early; early(){ echo hi; }'

Output:

bash: line 1: early: command not found

याचा अर्थ: आधी define.

  1. One-line body ला ;
bad(){ echo x }

Output:

bash: syntax error near unexpected token `}'

याचा अर्थ: } आधी ;.

3. Functions मधे arguments

हे का लागतं?

Server नाव किंवा path घेऊ शकणारा function प्रत्येक server आणि path साठी चालतो.

हे काय आहे?

Function मधे $1, $2, $#, $@ function चे स्वतःचे arguments, script चे नाहीत. Script चे arguments वापरायचे तर द्या: main "$@". $0 अजूनही script नाव; FUNCNAME[0] function नाव. Required arguments ${1:?usage} किंवा $# ने तपासा.

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

  1. दोन arguments.
add_user(){ echo "adding $1 to group $2"; }; add_user meera devs

Output:

adding meera to group devs

याचा अर्थ: $1 आणि $2 call चे.

  1. सगळे arguments.
each(){ for x in "$@"; do echo "- $x"; done; }; each 'Pune Camp' Kothrud

Output:

- Pune Camp
- Kothrud

याचा अर्थ: "$@" spaces ठेवतो.

  1. Required.
need(){ local f=${1:?need a file}; echo "using $f"; }; need

Output:

bash: 1: need a file

याचा अर्थ: Message सह थांबतो.

  1. स्वतःचं नाव.
whoami_fn(){ echo "in ${FUNCNAME[0]} with $# args"; }; whoami_fn a b

Output:

in whoami_fn with 2 args

याचा अर्थ: Log lines मधे उपयोगी.

4. return codes vs output

हे का लागतं?

Beginners return ने text परत द्यायचा प्रयत्न करतात. return फक्त 0-255 number देतो, success किंवा failure.

हे काय आहे?

return N function exit code N ने संपवतो (0 success). return नसेल तर शेवटच्या command चा code. Data परत द्यायचा तर echo किंवा printf ने print करा आणि पकडा: x=$(fn). Yes/no साठी code आणि values साठी output, म्हणजे function if मधे आणि $( ) मधे दोन्हीकडे वापरता येतो.

गरजवापरा
yes/noreturn 0 किंवा 1
a valueecho, x=$(fn)
many valuesnameref ने array
an error messageecho ... >&2

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

  1. Yes/no.
is_root(){ [[ $EUID -eq 0 ]]; }; if is_root; then echo root; else echo 'not root'; fi

Output:

not root

याचा अर्थ: शेवटचा test हाच return code.

  1. echo ने value.
disk_pct(){ df --output=pcent "${1:-/}" | tail -1 | tr -dc 0-9; }; p=$(disk_pct /); echo "root at $p%"

Output:

root at 41%

याचा अर्थ: $( ) ने पकडलं.

  1. return text सह fail.
bad(){ return hello; }; bad

Output:

bash: return: hello: numeric argument required

याचा अर्थ: return फक्त numbers.

  1. 255 पेक्षा जास्त wrap.
f(){ return 300; }; f; echo $?

Output:

44

याचा अर्थ: 300 mod 256 = 44.

5. local variables आणि scope

हे का लागतं?

i किंवा file नावाचा global variable बदलणारा function caller ला विचित्र प्रकारे बिघडवू शकतो.

हे काय आहे?

Default ने bash मधले सगळे variables global, functions मधे set केलेलेसुद्धा. local name=value variable फक्त function आणि तो call करतो त्या functions मधे ठेवतो. प्रत्येक helper variable local करा. लक्षात ठेवा: local x=$(cmd) cmd चा exit code लपवतो; तो लागत असेल तर local x; x=$(cmd).

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

  1. Global leak.
count=10; f(){ count=0; }; f; echo $count

Output:

0

याचा अर्थ: Caller ची value बदलली.

  1. local.
count=10; g(){ local count=0; }; g; echo $count

Output:

10

याचा अर्थ: Caller safe.

  1. लपलेला exit code.
h(){ local out=$(false); echo "code $?"; }; h

Output:

code 0

याचा अर्थ: local स्वतः यशस्वी.

  1. बरोबर प्रकार.
k(){ local out; out=$(false); echo "code $?"; }; k

Output:

code 1

याचा अर्थ: आता खरा code दिसतो.

6. अनेक values परत: nameref आणि globals

हे का लागतं?

कधी function ला array भरायचा किंवा दोन values set करायच्या असतात, आणि echo साठी अवघड.

हे काय आहे?

local -n ref=$1 (bash 4.3+) ref ला caller च्या variable चं दुसरं नाव बनवतो ज्याचं नाव तुम्ही देता. ref ला assign केलं की caller चा variable बदलतो. Loops टाळायला स्पष्ट नाव (caller सारखं नको). जुना प्रकार: RESULT सारखा documented global.

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

  1. Variable भरा.
get_os(){ local -n _out=$1; _out=$(. /etc/os-release; echo $ID); }; get_os os; echo $os

Output:

rhel

याचा अर्थ: Caller चा os set.

  1. Array भरा.
list_users(){ local -n _arr=$1; mapfile -t _arr < <(cut -d: -f1 /etc/passwd | head -3); }; list_users u; echo "${#u[@]}: ${u[*]}"

Output:

3: root bin daemon

याचा अर्थ: Arrays chapter 7 मधे.

  1. echo आणि read ने दोन values.
mem(){ free -m | awk '/Mem/{print $2, $3}'; }; read -r total used < <(mem); echo "$used of $total MB"

Output:

3100 of 7820 MB

याचा अर्थ: read output तोडतो.

7. main function आणि script structure

हे का लागतं?

वर settings, मधे functions आणि खाली main ला एक call असेल तर मोठे scripts वाचायला सोपे.

हे काय आहे?

क्रम: shebang, set -euo pipefail, constants, functions, मग शेवटची line main "$@". main चालण्याआधी bash पूर्ण file वाचतो म्हणून functions कोणत्याही क्रमात. Guard [[ ${BASH_SOURCE[0]} == $0 ]] && main "$@" file execute केली तरच main चालवतो, source केली तर नाही (testing साठी उपयोगी).

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

  1. Skeleton.
cat deploy.sh

Output:

#!/usr/bin/env bash
set -euo pipefail
APP=shop
log(){ echo "[$(date +%T)] $*"; }
build(){ log "building $APP"; }
ship(){ log "shipping to $1"; }
main(){ build; ship "${1:-stage}"; }
main "$@"

याचा अर्थ: Settings, functions, main.

  1. चालवा.
./deploy.sh prod

Output:

[10:15:02] building shop
[10:15:02] shipping to prod

याचा अर्थ: main ला script चे arguments.

  1. Source guard.
printf 'hi(){ echo hi; }\n[[ ${BASH_SOURCE[0]} == $0 ]] && hi\n' | tee g.sh; source g.sh; bash g.sh

Output:

hi

याचा अर्थ: Source ने चाललं नाही; execute ने चाललं.

8. Libraries: shared file source करा

हे का लागतं?

Logging, alerts आणि checks अनेक scripts ना लागतात. एका file मधे ठेवा आणि सगळीकडे load करा.

हे काय आहे?

source file (किंवा . file) file current shell मधे चालवतो, म्हणून त्याचे functions आणि variables मिळतात. Library मधे फक्त functions आणि constants, top level वर actions नाहीत. Script च्या relative शोधा: source "$(dirname "${BASH_SOURCE[0]}")/lib/common.sh". Clashes टाळायला function नावांना prefix (log_info, net_check). git मधे आणि servers साठी /usr/local/lib मधे ठेवा.

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

  1. Library लिहा.
mkdir -p lib; printf 'log_info(){ printf "%%s INFO %%s\\n" "$(date +%%F_%%T)" "$*"; }\nlog_err(){ printf "%%s ERROR %%s\\n" "$(date +%%F_%%T)" "$*" >&2; }\n' | tee lib/common.sh

याचा अर्थ: फक्त functions, top वर actions नाहीत.

  1. वापरा.
source lib/common.sh; log_info 'backup started'; log_err 'disk full'

Output:

2026-10-25_10:20:01 INFO backup started
2026-10-25_10:20:01 ERROR disk full

याचा अर्थ: दोन्ही functions load.

  1. Relative path.
here=$(dirname "${BASH_SOURCE[0]}"); echo "$here/lib/common.sh"

Output:

./lib/common.sh

याचा अर्थ: कोणत्याही folder मधून चालतं.

  1. Load झालेले functions.
declare -F | grep log_

Output:

declare -f log_err
declare -f log_info

याचा अर्थ: declare -F फक्त नावं.

9. trap: cleanup आणि error handlers

हे का लागतं?

Script fail झाला किंवा user ने Ctrl+C दाबलं तरी temp files आणि lock files काढायला हव्यात.

हे काय आहे?

trap 'commands' SIGNALS signal आला की commands चालवतो. EXIT प्रत्येक शेवटी (normal किंवा error). ERR command fail झाल्यावर (set -E सह functions मधेही). INT म्हणजे Ctrl+C, TERM म्हणजे kill. Cleanup function मधे: trap cleanup EXIT. $LINENO आणि $BASH_COMMAND error messages ला मदत.

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

  1. Cleanup.
bash -c 't=$(mktemp); cleanup(){ rm -f "$t"; echo "removed $t"; }; trap cleanup EXIT; echo work; false; echo never' 

Output:

work
removed /tmp/tmp.Q8x1

याचा अर्थ: इथे false ने काही थांबलं नाही तरी cleanup प्रत्येक exit ला चालतो.

  1. Error line.
bash -c 'set -eE; trap "echo failed at line \$LINENO: \$BASH_COMMAND" ERR; ls /nope'

Output:

ls: cannot access '/nope': No such file or directory
failed at line 1: ls /nope

याचा अर्थ: कोणती command fail झाली ते दाखवतो.

  1. Ctrl+C.
trap 'echo " stopping nicely"; exit 130' INT; sleep 100

Output:

^C stopping nicely

याचा अर्थ: Exit 130 = 128 + signal 2.

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

हे का लागतं?

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

हे काय आहे?

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

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

Team आणि शहरते कसं वापरतातCommand
Rutuja, bank ops, Pune40 scripts साठी shared log librarysource /usr/local/lib/rb/log.sh
James, SaaS, Austinmain "$@" structuremain "$@"
Sakura, retail, OsakaTemp files साठी trap cleanuptrap cleanup EXIT
Karim, oil and gas, Abu Dhabiif मधे वापरलेला is_up functionif is_up db1; then
Elena, healthcare, MilanHelpers मधे सगळीकडे locallocal f=$1
Ganesh, telecom, HyderabadArrays परत द्यायला namereflocal -n out=$1

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

हे का लागतं?

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

हे काय आहे?

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

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

दिसणारा errorका होतंFix कसा करायचा
command not found for my functionDefine आधी callआधी define किंवा शेवटी main.
main "$@"
syntax error near unexpected token `}'One line वर } आधी ; नाही; जोडा.
f(){ echo x; }
numeric argument requiredreturn text सहत्याऐवजी value echo.
echo "$val"
Caller's variable changedlocal नाहीlocal वापरा.
local i
$1 is empty in the functionScript args दिले नाहीत"$@" सह call.
main "$@"
No such file when sourcing libraryPath current folder relativeBASH_SOURCE वापरा.
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
Error code lostlocal x=$(cmd)Declare आणि assign वेगळे.
local x; x=$(cmd)
circular name referencenameref caller var सारखं नावUnique ref नाव.
local -n _ref=$1
Temp files left after Ctrl+Ctrap नाहीtrap cleanup EXIT.
trap cleanup EXIT

12. Troubleshooting step by step

हे का लागतं?

कोणते functions आहेत ते बघा, calls trace करा आणि return codes तपासा.

हे काय आहे?

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

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

  1. Functions list.
declare -F
  1. एक दाखवा.
declare -f log_info
  1. Function नावांसह trace.
PS4='+${FUNCNAME[0]:-main}:$LINENO: ' bash -x s.sh
  1. Call stack.
echo "${FUNCNAME[*]}"
  1. Return code.
myfn; echo $?
  1. Lint.
shellcheck s.sh lib/*.sh

13. Lab 1: छोटी library बनवा

हे का लागतं?

log, die आणि need_cmd functions सह lib/ops.sh बनवा, मग दोन scripts मधून वापरा.

हे काय आहे?

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

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

  1. Folder.
mkdir -p ~/lab42/lib && cd ~/lab42
  1. Library.
printf 'log(){ echo "[$(date +%%T)] $*"; }\ndie(){ echo "FATAL: $*" >&2; exit 1; }\nneed_cmd(){ command -v "$1" &>/dev/null || die "$1 not installed"; }\n' | tee lib/ops.sh
  1. Script A.
printf '#!/bin/bash\nsource "$(dirname "$0")/lib/ops.sh"\nneed_cmd tar\nlog "tar is ready"\n' | tee a.sh && chmod +x a.sh && ./a.sh

Output:

[10:30:11] tar is ready
  1. Script B नीट fail.
printf '#!/bin/bash\nsource "$(dirname "$0")/lib/ops.sh"\nneed_cmd nosuchtool\n' | tee b.sh && chmod +x b.sh && ./b.sh; echo $?

Output:

FATAL: nosuchtool not installed
1
  1. दुसरीकडून चालवा.
cd /tmp && ~/lab42/a.sh

Output:

[10:30:20] tar is ready
  1. Functions list.
bash -c 'source ~/lab42/lib/ops.sh; declare -F'

Output:

declare -f die
declare -f log
declare -f need_cmd

14. Lab 2: main, local आणि trap एकत्र

हे का लागतं?

main function, local variables, value परत देणारा function आणि temp file काढणारा trap सह report.sh लिहा.

हे काय आहे?

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

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

  1. लिहा.
cd ~/lab42 && printf '#!/usr/bin/env bash\nset -euo pipefail\ntmp=$(mktemp)\ncleanup(){ rm -f "$tmp"; echo "cleaned"; }\ntrap cleanup EXIT\ncount_files(){ local d=$1; find "$d" -maxdepth 1 -type f | wc -l; }\nmain(){ local dir=${1:-/etc}; local n; n=$(count_files "$dir"); echo "$dir has $n files" | tee "$tmp"; }\nmain "$@"\n' | tee report.sh && chmod +x report.sh
  1. चालवा.
./report.sh /etc

Output:

/etc has 94 files
cleaned
  1. Default.
./report.sh

Output:

/etc has 94 files
cleaned
  1. Failure तरी clean.
./report.sh /nope; echo $?

Output:

find: '/nope': No such file or directory
cleaned
1
  1. Temp गेली.
ls /tmp/tmp.* 2>/dev/null | wc -l

Output:

0
  1. dir local राहिला.
bash -c 'source <(sed "\$d" report.sh); main /tmp; echo "[${dir:-}]"'

Output:

/tmp has 3 files
[]
cleaned

Ravindra Bagale's Tip

Make every variable inside a function local, use return only for success or failure, and echo the data you want to give back. Put shared functions in a library and source it with a path built from BASH_SOURCE.

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

प्र. name() की function name?

bash मधे दोन्ही; name() portable.

प्र. Functions एकमेकांना call?

हो, आणि स्वतःला (recursion).

प्र. String कसा परत?

echo करा आणि $( ).

प्र. $( ) slow करतो?

Subshell fork; बहुतेक scripts साठी ठीक.

प्र. source vs script चालवणं?

source current shell मधे.

प्र. Function child scripts ला?

export -f name.

प्र. Function unset?

unset -f name.

प्र. Libraries कुठे?

Repo lib/ किंवा /usr/local/lib.

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

Beginner level

प्र. Function define?

name() { ...; }.

प्र. Function मधला पहिला argument?

$1.

प्र. Library load?

source file.

प्र. Function exit code?

return N किंवा शेवटची command.

Mid level level

प्र. return vs echo?

Code vs data.

प्र. local का?

Globals बदलणं टाळायला.

प्र. trap EXIT काय करतो?

कोणत्याही exit ला cleanup.

प्र. Script args main ला?

main "$@".

Senior level

प्र. Shared bash library design?

Prefixed functions, top-level actions नाहीत, source guard, versioned, CI मधे shellcheck.

प्र. local x=$(cmd) problem?

Exit code लपतो; वेगळं करा.

प्र. Arrays परत?

nameref local -n किंवा mapfile ने lines.

प्र. Scripts testable?

Functions + source guard; bats ने test.

घरी करून बघा

  1. 0 किंवा 1 देणारा is_port_open HOST PORT function लिहा आणि if मधे वापरा.
  2. तो lib/net.sh मधे हलवा आणि दोन वेगळ्या scripts मधून source करा.

सराव Tasks

या scenario बद्दल

Ahmednagar Sugar Co-op हा अहिल्यानगर मधला साखर सहकारी व्यवसाय आहे. Balasaheb Pawar त्यांचं IT व cloud काम पाहतात. त्यांचे पैसे INR मध्ये मोजले जातात आणि AWS resources ap-south-1 Region मध्ये चालतात. खालच्या tasks मध्ये तुम्ही या lesson मध्ये शिकलेलं वापरून त्यांची खरी कामं करता. सगळी नावं, account IDs, IP addresses व numbers सरावासाठी काल्पनिक आहेत.

0 / 21

सोपं Define

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: Namaste print करणारा hello function एका line वर.

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

सोपं Function आहे का

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: backup_now कोणत्या प्रकारची command आहे.

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

सोपं Functions list

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: compgen ने सगळ्या defined functions ची नावं.

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

सोपं Local variable

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: Function मधे पहिल्या argument ने set local dir variable.

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

सोपं Failure return

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: Function मधून code 1 सह failure.

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

सोपं run_all call

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: सगळे script arguments देऊन run_all call.

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

सोपं Library load

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: lib/alerts.sh current shell मधे load.

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

सोपं Traps list

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: सध्या set असलेले traps दाखवा.

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

मध्यम Code दाखवा

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: send_sms function चा code दाखवा.

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

मध्यम Output पकडा

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: get_ip function चा output addr मधे.

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

मध्यम if मधे function

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: is_weekend if मधे चालवा आणि success वर rest print.

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

मध्यम Required arg

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: Function मधे local f $1, नाहीतर "need path" सह थांबा.

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

मध्यम Function export

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: notify function child shells ना export.

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

मध्यम Function काढा

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: old_sync function delete.

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

मध्यम Function नाव

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: Function मधे त्याचं स्वतःचं नाव print.

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

मध्यम Cleanup trap

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: Script exit होईल तेव्हा tidy function चालवा.

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

मध्यम Ctrl+C trap

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: Ctrl+C वर bye print करून exit 130.

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

अवघड Code ठेवा

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: Command output assign करण्याआधी local out वेगळ्या statement ने declare.

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

अवघड Relative library

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: चालू script च्याच folder मधून helpers.sh source, BASH_SOURCE वापरून.

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

अवघड Nameref

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: Function मधे _dst ला $1 मधे दिलेल्या variable चा name reference.

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

अवघड Error trap

का: Shared functions BIZ चे scripts छोटे आणि एकसारखे ठेवतात.

काय करायचं: ERR trap functions मधेही चालेल असं, exit-on-error सोबत.

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

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

  1. name(){ ...; } define; नावाने call.
  2. आतले $1, $@ function चे arguments.
  3. return code; echo data.
  4. local variables private.
  5. source library current shell मधे load.
  6. शेवटी main "$@", safety साठी trap cleanup EXIT.

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

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

4 steps मधे Functions. Define: name() { }. आत args $1, $2. Variables साठी local. return ने status.
उजळणी card: 4 steps मधे Functions · PNG download करा · Card page उघडा
Script library. Helpers एका file मधे. आत फक्त functions. Load करा. शेवटी main call.
उजळणी card: Script library · PNG download करा · Card page उघडा
return vs exit. Function सोडतो. / पूर्ण script संपवतो.
तुलना: return vs exit · PNG download करा · Card page उघडा
source vs ./script. तोच shell. / नवा child shell.
तुलना: source vs ./script · PNG download करा · Card page उघडा
Function मधे return vs echo?. return 0-255 status. echo capture साठी data. Capture.
मुलाखत card: Function मधे return vs echo? · PNG download करा · Card page उघडा
local का?. Default ने variables global. local आत ठेवतो. उदाहरण.
मुलाखत card: local का? · PNG download करा · Card page उघडा
source vs ./script?. source current shell मधे. ./script नवा shell. Helpers load.
मुलाखत card: source vs ./script? · PNG download करा · Card page उघडा
Exit वर clean up?. trap signals वर code चालवतो. EXIT कोणत्याही शेवटी. उदाहरण.
मुलाखत card: Exit वर clean up? · PNG download करा · Card page उघडा

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