Delete Set public Set private Add tags Delete tags
  Add tag   Cancel
  Delete tag   Cancel
  • • DevOps notes •
  •  
  • AI
  • Tags
  • Login

Shell Scripting Basics/shaare/wcZcmg

  • linux
  • linux

First Line of Script

  • #!/bin/bash → defines the shell interpreter

Comments

  • Use # for commenting

Common Elements

  • Commands: echo, cp, etc.
  • Statements: if, while, for
  • +x → make the script executable
  • Use absolute path to run: /home/userdir/myscript.bash

Variable Definition

a=hello
b=mytext
  • Usage:
echo "text1 $a"
echo "text2 $b"
  • Example:
a=$(hostname)
echo $a  # => myfirstlinux

Input / Output Variable

read myinputvariable
echo "name $myinputvariable"

If Else Statement

if [[ $count -eq 100 ]]; then
  echo "count is 100"
else
  echo "no"
fi

File Existence Check

if [[ -e /file.txt ]]; then
  echo "file exists"
fi
3 months ago Permalink
cluster icon
  • Vi Editor : Vi Editor Commands i → insert mode I → insert at line start ESC → escape to command mode R → replace mode x → delete character dd → delete line yy → ...
  • SED: Stream Editor for Text Manipulation : Basic Replace Syntax: sed -i 's/KENNY/LENNY/g' filename Substitute all occurrences of "KENNY" with "LENNY" Delete Line Containing String: s...
  • Alias Setup : alias l="ls -al" → define alias unalias l → remove alias Persistent Alias Config User-specific: ~/.bashrc Global: /etc/bashrc Add: alias l="ls -a...
  • Process monitoring : ps Command ps → Current shell process status ps -E → All running processes ps aux → All in BSD style with details ps -EF → Running process with detai...
  • Disk usage, logging, shutdown, hostname : Disk and Memory Info df → disk partition info df -h → human-readable format du → size of file/directory free → memory info cat /proc/cpuinfo → CPU in...


(19)
Filter untagged links
Fold Fold all Expand Expand all Are you sure you want to delete this link? Are you sure you want to delete this tag? The personal, minimalist, super-fast, database free, bookmarking service by the Shaarli community