Skip to content

Instantly share code, notes, and snippets.

@epynic
Last active September 16, 2022 16:43
Show Gist options
  • Select an option

  • Save epynic/0743de36a2b322b267fe8b9f4b43bc00 to your computer and use it in GitHub Desktop.

Select an option

Save epynic/0743de36a2b322b267fe8b9f4b43bc00 to your computer and use it in GitHub Desktop.

Revisions

  1. epynic revised this gist Jan 31, 2019. 7 changed files with 102 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions debugging.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    #debugging shellscripts -x would show the scrips line by line and output followed by
    #!/bin/bash -x
    TEST_VAR="test"
    echo "$TEST_VAR"
    #custom
    #set -x / start debugginh
    #set +x / stop
    #-ex exit on stop
    #-v prints shell before substitution are applied
    #-vx
    #DEBUG="echo"
    #$DEBUG ls
    14 changes: 14 additions & 0 deletions decisions_case.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    #!/bin/bash
    #case statement example
    read -p "Enter y or n:" ANSWER
    case "$ANSWER" in
    [yY]|[yY][eE][sS])
    echo "You answered yes."
    ;;
    [nN]|[nN][oO])
    echo "You answered no."
    ;;
    *)
    echo "Invalid answer."
    ;;
    esac
    21 changes: 21 additions & 0 deletions decisions_if.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #!/bin/bash
    #decisions
    HOST_NAME="windows"
    #simple if
    if [ $HOST_NAME = "ubuntu" ]; then
    echo "Ubuntu for humans"
    fi
    #if else
    if [ $HOST_NAME = "ubuntu" ]; then
    echo "Ubuntu for humans"
    else
    echo "Windows for !humans"
    fi
    #if elif else
    if [ $HOST_NAME = "ubuntu" ]; then
    echo "Ubuntu for humans"
    elif [ $HOST_NAME = "windows" ]; then
    echo "We !Love Windows"
    else
    echo "Windows for !humans"
    fi
    18 changes: 18 additions & 0 deletions file_operations.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    #!/bin/bash
    #file operators
    FILE="basic.sh"
    if [ -e $FILE ]; then
    echo "File Exists"
    else
    echo "No File Found"
    fi

    #-d,e,r,w,x
    #string operations
    #arithmetic operations are allowed
    #arg1 -eq arg2 - equalto
    #arg1 -ne arg2 - !equalto
    #arg1 -lt arg2 - less than
    #arg1 -le arg2 - less than equalto
    #arg1 -gt arg2 - greater than
    #arg1 -ge arg2 - greater than equalto
    17 changes: 17 additions & 0 deletions logging_example.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    #syslog

    #logger -p user.info "$MESSAGE"
    #logger -i -t randomly -p user.info "$MESSAGE"
    #while loop
    while [ "$CORRECT" != "y" ]
    do
    read -p "Whats your name ? " NAME
    read -p "Is your Name: $NAME - correct ?" CORRECT
    done
    #read file line by line
    LINE_NUM=1
    while read LINE
    do
    echo "${LINE_NUM}: ${LINE}"
    ((LINE_NUM++))
    done < /etc/fstab
    5 changes: 5 additions & 0 deletions loops.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    #!/bin/bash
    #loops
    COLORS="red green blue"
    for COLOR in $COLORS
    do
    15 changes: 15 additions & 0 deletions wildcard_examples.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    #!/bin/bash
    #wildcards *.txt match zero or more characters
    #?.txt match exactly one character
    ls *\? #file end with ?
    ls ? #file 1 character
    ls -l *.txt
    ls c[aeiou]t #character class
    #can use this with mv cp rm
    #copy .html files to another folder - wildcards
    cd /var/www
    for FILE in *.html
    do
    echo "Copying $FILE"
    cp $FILE /var/www-backup
    done
  2. epynic created this gist Jan 31, 2019.
    11 changes: 11 additions & 0 deletions define_variables.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    #!/bin/bash

    #declare variable
    SERVER_NAME="nginx"
    echo $SERVER_NAME
    echo "We are running on $SERVER_NAME"
    echo "We are ${SERVER_NAME}er";

    #command output to variable
    SERVER_NAME=$(hostname)
    echo "We are running on $SERVER_NAME"