ארגז כלים — פקודות shell לאבחון
לעקוב אחרי script שנכשל
Section titled “לעקוב אחרי script שנכשל”bash -x script.sh # prints every command before running itset -x # inside a script: start tracing (set +x stops)trap 'echo "failed at line $LINENO"' ERR # put under set -e: names the failing linekubectl get ns nope; echo "rc=$?" # $? must be read on the very next lineאיך מוודאים: trap מדפיס failed at line 5 והמספר הוא השורה ב-script. $? מתאפס אחרי כל פקודה, אפילו echo.
בדיקות: קובץ, פקודה, תנאי
Section titled “בדיקות: קובץ, פקודה, תנאי”| רוצה לדעת | כתוב |
|---|---|
| הפקודה קיימת? | command -v kubectl >/dev/null || echo missing |
| קובץ רגיל / תיקייה | test -f <FILE> / test -d <DIR> |
| קובץ לא ריק / קריא | test -s <FILE> / test -r <FILE> |
| כמה תנאים יחד | [[ -f <FILE> && "$X" == dev* ]] |
[[ ]] הוא של bash: מרכאות לא חובה, תומך ב-&&, ||, ו-== עם תבנית. [ ] הוא test הישן: שם חייבים מרכאות סביב משתנים ו--a/-o במקום &&/||.
לחפש ולחתוך טקסט
Section titled “לחפש ולחתוך טקסט”grep -q 'text' <FILE> && echo found # -q: no output, only exit codegrep -c 'text' <FILE> # count matching linesgrep -n 'text' <FILE> # with line numbersgrep -E '^(dev|prod):' <FILE> # extended regex (alternation)grep -F 'a.b[0]' <FILE> # literal string, no regexsed -n '10,20p' <FILE> # print lines 10 to 20 onlyawk '{print $1}' <FILE> # first whitespace-separated columncut -d: -f1 /etc/passwd # first field split by ':'kubectl get pods -o name | xargs -n1 echo # one argument per command runאיך מוודאים: grep -q מחזיר exit 0 כשנמצא ו-1 כשלא. grep -c תמיד מדפיס מספר (גם 0).
לשמור, להשוות, לפענח
Section titled “לשמור, להשוות, לפענח”helm template <RELEASE> <CHART_DIR> | tee /tmp/render.yaml | grep -c 'kind:' # save AND pipe on (-a appends)diff <(grep image: a.yaml) <(grep image: b.yaml) # compare two outputs, no temp filesecho -n '<BASE64>' | base64 -d # decode (-n: no newline added)curl -s http://<HOST>/info | jq -r '.version' # -r: raw string, no quotesTAG="dev-$(date -u +%Y%m%d)" # e.g. dev-20261004cat -A <FILE> # ^M = CRLF, $ = line end, spaces visiblediff יוצא עם 1 כשיש הבדל ו-0 כשאין. ב-cat -A: ^M$ בסוף שורה = CRLF (תיקון); רווח לפני $ = רווח מיותר.
curl: לבדוק שירות
Section titled “curl: לבדוק שירות”curl -fsS http://<HOST>/ready # -f fail on HTTP error, -sS quiet but show errorscurl -s -o /dev/null -w '%{http_code}\n' http://<HOST>/ready # only the status codecurl -k --resolve <HOST>:<PORT>:<VM_IP> https://<HOST>:<PORT>/info # TLS via NodePort without /etc/hostsאיך מוודאים: 200 ב-http_code. 000 = לא היה חיבור בכלל (DNS/פורט), לא שגיאת HTTP. עוד: kubernetes/networking.