Skip to content
Menu
myCloud myCloud

Personal short notes about Cloud

  • XMPie
  • AWS
    • AWS Topics
    • AWS Architecture
    • AWS CLI
    • AWS Health
    • AWS Policies
    • AWS Cost
  • CHEAT SHEETS
myCloud myCloud

Personal short notes about Cloud

Bash scripting – CS

By mikado on November 3, 2022November 7, 2022

#!/bin/bash

  • chmod +x <filename>
  • to run script: ./<filename>

.bashrc

  • customize your bash
  • home directory is created from /etc/skel/, this includes default .bashrc and .profile
/etc/skel/.bashrc
  • Create/remove alias (b.bashrc)
alias extip='curl icanhazip.com'
unalias

Variables

  • in caps
  • no spaces near =
HELLO="Hello World"
echo $HELLO

Execution

  • df -h
  • watch df- h –>every 2s

Numbers

expr
  • * is nor for multiplying (* wildcard operator) so use: \*
  • let <variable>

tar

  • To extract a .tgz file
tar -xvzf /path/to/yourfile.tgz
  • To different directory
tar -xvzf /path/to/yourfile.tgz -C /path/where/to/extract/

if

if [ $MYNUM -eq 200 ]
then
else
fi

case

#!/bin/bash

echo "what is favorite color?"
echo "1 - Black"
echo "2 - Red"
echo "3 - White"
echo "4 - Blue"

read color;

case $color in
    1) echo "Night is Black";;
    2) echo "Blood is Red";;
    3) echo "Snow is White";;
    4) echo "Sky is Blue";;
    *) echo "no relevant!";;
esac

while

#!/bin/bash

mika=1

while [ $mika -le 10 ]
do
    echo $mika
    mika=$(( $mika + 1))
    spleep 0.5
done

File/Directory Exists?

  • File
if [ -f ~/myfile ]
  • Directory
if [ -d ~/myfolder ]

Universal update scripts (<>distros)

#!/bin/bash

cd /etc

## testing arch based
if [ -d /etc/pacman.d ]
then 
    ## Run arch update cmd
    sudo pacman -Syu
fi

## Testing debian-based
if [ -d /etc/apt ]
then 
    ## Run arch update cmd
    sudo apt-get update && sudo apt-get dist-upgrade -y
fi

Standard Input, Output and Error

  • stdin (0)
    • read <variable>
  • stdout (1)
    • > and >> (append)
  • stderror (2)
    • ls /root 2> errors.txt
  • /dev/null

File system

  • echo $PATH
    • /usr/local/sbin
    • /usr/local/bin
    • /usr/sbin
    • /usr/bin
    • /sbin
    • /bin

File Hierarchy Standard (FHS) recommendation

  • for users
/usr/local/bin
  • for admins
/usr/local/sbin
  • To add path: ~/bin folder edit .bash_profile
PATH=$PATH:$HOME/bin
export PATH

df

df -Th

fdisk

fdisk -l
  • mount
mkfs.xfs /dev/xvdf
mount /dev/xvdf /myfoldermount
  • to make permanently accessible, add an entry in:
/etc/fstab
/dev/xvdf   /myfoldermount   xfs   defaults   0   0 
mount -a

  • check if disk formatted
lbkid
lsblk

du

  • list all files and directory size
du -sh * | sort

lsof (list open files)

  • count list open files
sudo lsof | wc -l
  • for specific user mika
sudo lsof -u mika

ip – ifconfig

/etc/sysconfig/network-scripts/ifcfg-eth0

mlocate

yum install mlocate
updatedb

logged-in users

w

Sending emails

  • Need to install packages:
sudo apt-get install sendEmail libio-socket-ssl-perl libnet-ssleay-perl
  • Send email
sendEmail -f <fromEmail> -t <toEmail> -u "This is the subject" -m "This is the message" -s smtp.googlemail.com:587 -xu <user> x -xp <password> -o tls=yes
  • Send email with disk usage report for sda1
#!/bin/bash

SMTPFROM=jahec2@gmail.com
SMTPTO=jahec2@gmail.com
SMTPSERVER=smtp.googlemail.com:587
SMTPUSER=jahec2
SMTPPASS="<password>"
MESSAGEBODY="This is the message"
SUBJECT="Alert from df -h"

df -h | grep Filesystem > /tmp/diskusage.txt
df -h | grep sda1 >> /tmp/diskusage.txt

sendEmail -f $SMTPFROM -t $SMTPTO -u $SUBJECT -o message-file=/tmp/diskusage.txt -s $SMTPSERVER -xu $SMTPUSER x -xp $SMTPPASS -o tls=yes

rm /tmp/diskusage.txt

Category: CHEAT SHEETS, Linux, SCRIPTS

Categories

  • AWS (4)
  • AWS Architecture (8)
  • AWS CLI (5)
  • AWS Cost (3)
  • AWS Health (4)
  • AWS Policies (2)
  • AWS Topics (24)
  • CHEAT SHEETS (16)
  • Container (21)
  • Datadog (4)
  • Jenkins (2)
  • Linux (9)
  • Microsoft (7)
  • Python (1)
  • SCRIPTS (9)
  • Terraform (5)
  • XMPie (6)
©2025 myCloud
Click to Copy