Daily Weekly Monthly

Weekly Shaarli

Week 29 (July 14, 2025)

Bash

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

Check If a Variable Matches a Value

a=$(date | awk '{print $1}')
if [[ "$a" == "Mon" ]]; then
  echo "Today is $a"
else
  echo "Today is not Monday"
fi

Check Multiple Values

if [[ "$a" == "Monday" ]] || [[ "$a" == "Tuesday" ]]; then
  echo "It's early week"
fi

File Operation Tests

  • -s → file exists and is not empty
  • -f → file exists and is a regular file
  • -d → directory exists
  • -x → file is executable
  • -w → file is writable
  • -r → file is readable

Comparison Operators

  • -eq → equal (numeric)
  • = → equal (string)
  • -ne → not equal (numeric)
  • != → not equal (string)
  • -lt → less than
  • -le → less than or equal
  • -gt → greater than
  • -ge → greater than or equal
  • $((...)) → arithmetic evaluation

For Loop Examples

for i in 1 2 3 4 5; do
  echo "Welcome $i times"
done

for i in eat run jump play; do
  echo "User $i"
done

for i in {1..5}; do
  touch $i
  echo "File $i created"
done

Indexed For Loop Example

i=1
for day in Mon Tue Wed Thu Fri; do
  echo "Weekday $((i++)) : $day"
done

User Listing with For + AWK

i=1
for username in $(awk -F: '{print $1}' /etc/passwd); do
  echo "Username $((i++)) : $username"
done

While Loop

c=1
while [[ $c -le 5 ]]; do
  echo "Welcome $c times"
  ((c++))
done

Case Statement

echo "Choose function"
echo "A"
echo "B"
read choices  # could also use: read -s password for silent input

case $choices in
  A) date;;
  B) ls;;
  *) echo "Invalid choice";;
esac

Ping Check with Conditional

hosts="192.168.0.1"
ping -c1 $hosts &> /dev/null

if [[ $? -eq 0 ]]; then
  echo "$hosts OK"
else
  echo "$hosts NOT OK"
fi
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 -al" at end of file
    • Save file

Command History

  • Location: ~/.bash_history
  • View: history
  • Recall specific: !4 → run whoami (4th command in history)
Screen & Tmux

Screen (Terminal Multiplexer)

  • Multi-terminal sessions in one window
  • Alt+A | → split vertical
  • Alt+A Shift+S → split horizontal
  • Alt+A Tab → switch windows
  • Alt+A C → create new session
  • screen -r → reconnect to detached session
  • screen -r [id] → recover session by ID

TMUX (Terminal Multiplexer)

  • Ctrl+B Shift+% → split window horizontally
  • Ctrl+B D → detach session
  • Ctrl+B Arrow Keys → move between panes
  • Ctrl+B Shift+" → split window vertically
  • tmux ls → list sessions
  • tmux a → resume last session
  • tmux new -s name → create named session
  • Ctrl+B C → new window
  • Ctrl+B N → switch to next window
  • tmux attach-session -t 0 → attach session ID 0
  • Ctrl+B , → rename window
  • tmux kill-session -t 0 → kill session ID 0

TMUX Advanced Commands

  • tmux new -s name → new named session
  • Ctrl+B C → new window
  • Ctrl+B N → switch to next window
  • tmux attach-session -t 0 → attach session 0
  • Ctrl+B , → rename window
  • tmux kill-session -t 0 → kill session 0
Shell Scripting Basics

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
Special Permissions (SetUID, SetGID, Sticky Bit)
  • chmod u+s xyz.sh → add SetUID (user-level special permission)
  • chmod g+s xyz.sh → add SetGID (group-level special permission)
  • chmod u-s xyz.sh → remove SetUID
  • chmod g-s xyz.sh → remove SetGID
  • find / -perm /6000 -type f → find all executables with SetUID/SetGID

⚠️ SetUID/SetGID works only for C/C++ compiled binaries, not Bash scripts.

Sticky Bit

  • Protects files from deletion by non-owners
  • Example: ls / → shows sticky bit as t in rwxrwxrwt
  • chmod +t file/dir → assign sticky bit

    • Only the creator can delete contents of that folder, even with full permissions