Skip to content

Instantly share code, notes, and snippets.

@bradtraversy
Last active October 9, 2025 10:52
Show Gist options
  • Save bradtraversy/ac3b1136fc7d739a788ad1e42a78b610 to your computer and use it in GitHub Desktop.
Save bradtraversy/ac3b1136fc7d739a788ad1e42a78b610 to your computer and use it in GitHub Desktop.

Revisions

  1. bradtraversy revised this gist May 17, 2018. 1 changed file with 85 additions and 94 deletions.
    179 changes: 85 additions & 94 deletions myscript.sh
    Original file line number Diff line number Diff line change
    @@ -1,146 +1,137 @@
    #! /bin/bash

    ## ECHO COMMAND
    # echo "Hello World!"
    # ECHO COMMAND
    # echo Hello World!

    # VARIABLES
    # Uppercase by convention
    # Letters, numbers, underscores
    NAME="Bob"
    # echo "My name is $NAME"
    # echo "My name is ${NAME}"

    ### VARIABLES
    ## Uppercase by convention
    ## Can include letters, numbers and underscores
    # NAME="Jack"
    # echo "Hello, my name is $NAME"
    # echo "Hello, my name is ${NAME}"
    # USER INPUT
    # read -p "Enter your name: " NAME
    # echo "Hello $NAME, nice to meet you!"


    ## USER INPUT
    # read -p "Enter You Name: " NAME
    # echo "Hello $NAME, nice to meet you"


    ## CONDITIONALS

    ## SIMPLE IF STATEMENT
    # SIMPLE IF STATEMENT
    # if [ "$NAME" == "Brad" ]
    # then
    # echo "Your name is Brad"
    # echo "Your name is Brad"
    # fi

    ## IF ELSE
    # IF-ELSE
    # if [ "$NAME" == "Brad" ]
    # then
    # echo "Your name is Brad"
    # echo "Your name is Brad"
    # else
    # echo "Your name is NOT Brad"
    # echo "Your name is NOT Brad"
    # fi

    ## ELSE-IF (elif)
    # ELSE-IF (elif)
    # if [ "$NAME" == "Brad" ]
    # then
    # echo "Your name is Brad"
    # elif [ "$NAME" == "Jack" ]
    # then
    # echo "Your name is Jack"
    # echo "Your name is Brad"
    # elif [ "$NAME" == "Jack" ]
    # then
    # echo "Your name is Jack"
    # else
    # echo "Your name is NOT Brad"
    # echo "Your name is NOT Brad or Jack"
    # fi

    ## CASE STATEMENT
    # read -p "Are you 21 or over? Y/N: " ANSWER
    # case "$ANSWER" in
    # [yY] | [yY][eE][sS])
    # echo "You can have a beer :)"
    # ;;
    # [nN] | [nN][oO])
    # echo "Sorry, no drinking :("
    # ;;
    # *)
    # echo "Please answer with Y/yes or N/no"
    # ;;
    # esac

    # COMPARISON
    # NUM1=31
    # NUM2=5
    # if [ "$NUM1" -gt "$NUM2" ]
    # then
    # echo "$NUM1 is greater than $NUM2"
    # else
    # echo "$NUM1 is less than $NUM2"
    # echo "$NUM1 is greater than $NUM2"
    # else
    # echo "$NUM1 is less than $NUM2"
    # fi

    ########
    # val1 -eq val2 Returns true if the values are equal
    # val1 -ne val2 Returns true if the values are not equal
    # val1 -gt val2 Returns true if val1 is greater than val2
    # val1 -ge val2 Returns true if val1 is greater than or equal to val2
    # val1 -lt val2 Returns true if val1 is less than val2
    # val1 -le val2 Returns true if val1 is less than or equal to val2
    # val1 -eq val2 Returns true if the values are equal
    # val1 -ne val2 Returns true if the values are not equal
    # val1 -gt val2 Returns true if val1 is greater than val2
    # val1 -ge val2 Returns true if val1 is greater than or equal to val2
    # val1 -lt val2 Returns true if val1 is less than val2
    # val1 -le val2 Returns true if val1 is less than or equal to val2
    ########

    ## FILE CONDITIONALS
    # FILE CONDITIONS
    # FILE="test.txt"

    # if [ -f "$FILE" ]
    # if [ -e "$FILE" ]
    # then
    # echo "$FILE is a file"
    # else
    # echo "$FILE is NOT a file"
    # echo "$FILE exists"
    # else
    # echo "$FILE does NOT exist"
    # fi

    ########
    # -d file True if the file is a directory
    # -e file True if the file exists (note that this is not particularly portable, thus -f is generally used)
    # -f file True if the provided string is a file
    # -g file True if the group id is set on a file
    # -r file True if the file is readable
    # -s file True if the file has a non-zero size
    # -u True if the user id is set on a file
    # -w True if the file is writable
    # -x True if the file is an executable
    # -d file True if the file is a directory
    # -e file True if the file exists (note that this is not particularly portable, thus -f is generally used)
    # -f file True if the provided string is a file
    # -g file True if the group id is set on a file
    # -r file True if the file is readable
    # -s file True if the file has a non-zero size
    # -u True if the user id is set on a file
    # -w True if the file is writable
    # -x True if the file is an executable
    ########

    ## FOR LOOP
    # NAMES="Brad John Harry Karen Sam"
    #CASE STATEMENT
    # read -p "Are you 21 or over? Y/N " ANSWER
    # case "$ANSWER" in
    # [yY] | [yY][eE][sS])
    # echo "You can have a beer :)"
    # ;;
    # [nN] | [nN][oO])
    # echo "Sorry, no drinking"
    # ;;
    # *)
    # echo "Please enter y/yes or n/no"
    # ;;
    # esac

    # SIMPLE FOR LOOP
    # NAMES="Brad Kevin Alice Mark"
    # for NAME in $NAMES
    # do
    # echo "Hello ${NAME}"
    # do
    # echo "Hello $NAME"
    # done

    ## RENAME A BUNCH OF FILES USING A FOR LOOP
    # FILES=$(ls *txt)
    # FOR LOOP TO RENAME FILES
    # FILES=$(ls *.txt)
    # NEW="new"
    # for FILE in $FILES
    # do
    # echo "Renaming $FILE to new-$FILE"
    # mv $FILE $NEW-$FILE
    # for FILE in $FILES
    # do
    # echo "Renaming $FILE to new-$FILE"
    # mv $FILE $NEW-$FILE
    # done

    ## WHILE LOOP - Read through a file line by line
    # WHILE LOOP - READ THROUGH A FILE LINE BY LINE
    # LINE=1
    # while read -r CURRENT_LINE
    # do
    # echo "${LINE}: $CURRENT_LINE"
    # ((LINE++))
    # done < "./new-test.txt"


    ## FUNCTION
    # function sayHello () {
    # echo "Hello World"
    # do
    # echo "$LINE: $CURRENT_LINE"
    # ((LINE++))
    # done < "./new-1.txt"

    # FUNCTION
    # function sayHello() {
    # echo "Hello World"
    # }

    # sayHello

    ## FUNCTION WITH PARAMS
    # FUNCTION WITH PARAMS
    # function greet() {
    # echo "Hello, I am $1 and I am $2"
    # echo "Hello, I am $1 and I am $2"
    # }

    # greet "Brad" "36"


    # CREATE AND WRITE TO A FILE
    # mkdir test
    # touch "test/hello.txt"
    # echo "Hello World" >> "test/hello.txt"
    # echo "Created test/hello.txt"
    # CREATE FOLDER AND WRITE TO A FILE
    # mkdir hello
    # touch "hello/world.txt"
    # echo "Hello World" >> "hello/world.txt"
    # echo "Created hello/world.txt"
  2. bradtraversy revised this gist May 16, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion myscript.sh
    Original file line number Diff line number Diff line change
    @@ -68,12 +68,14 @@
    # echo "$NUM1 is less than $NUM2"
    # fi

    ########
    # val1 -eq val2 Returns true if the values are equal
    # val1 -ne val2 Returns true if the values are not equal
    # val1 -gt val2 Returns true if val1 is greater than val2
    # val1 -ge val2 Returns true if val1 is greater than or equal to val2
    # val1 -lt val2 Returns true if val1 is less than val2
    # val1 -le val2 Returns true if val1 is less than or equal to val2
    ########

    ## FILE CONDITIONALS
    # FILE="test.txt"
    @@ -85,6 +87,7 @@
    # echo "$FILE is NOT a file"
    # fi

    ########
    # -d file True if the file is a directory
    # -e file True if the file exists (note that this is not particularly portable, thus -f is generally used)
    # -f file True if the provided string is a file
    @@ -94,7 +97,7 @@
    # -u True if the user id is set on a file
    # -w True if the file is writable
    # -x True if the file is an executable

    ########

    ## FOR LOOP
    # NAMES="Brad John Harry Karen Sam"
  3. bradtraversy created this gist May 16, 2018.
    143 changes: 143 additions & 0 deletions myscript.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,143 @@
    #! /bin/bash

    ## ECHO COMMAND
    # echo "Hello World!"


    ### VARIABLES
    ## Uppercase by convention
    ## Can include letters, numbers and underscores
    # NAME="Jack"
    # echo "Hello, my name is $NAME"
    # echo "Hello, my name is ${NAME}"


    ## USER INPUT
    # read -p "Enter You Name: " NAME
    # echo "Hello $NAME, nice to meet you"


    ## CONDITIONALS

    ## SIMPLE IF STATEMENT
    # if [ "$NAME" == "Brad" ]
    # then
    # echo "Your name is Brad"
    # fi

    ## IF ELSE
    # if [ "$NAME" == "Brad" ]
    # then
    # echo "Your name is Brad"
    # else
    # echo "Your name is NOT Brad"
    # fi

    ## ELSE-IF (elif)
    # if [ "$NAME" == "Brad" ]
    # then
    # echo "Your name is Brad"
    # elif [ "$NAME" == "Jack" ]
    # then
    # echo "Your name is Jack"
    # else
    # echo "Your name is NOT Brad"
    # fi

    ## CASE STATEMENT
    # read -p "Are you 21 or over? Y/N: " ANSWER
    # case "$ANSWER" in
    # [yY] | [yY][eE][sS])
    # echo "You can have a beer :)"
    # ;;
    # [nN] | [nN][oO])
    # echo "Sorry, no drinking :("
    # ;;
    # *)
    # echo "Please answer with Y/yes or N/no"
    # ;;
    # esac

    # COMPARISON
    # NUM1=31
    # NUM2=5
    # if [ "$NUM1" -gt "$NUM2" ]
    # then
    # echo "$NUM1 is greater than $NUM2"
    # else
    # echo "$NUM1 is less than $NUM2"
    # fi

    # val1 -eq val2 Returns true if the values are equal
    # val1 -ne val2 Returns true if the values are not equal
    # val1 -gt val2 Returns true if val1 is greater than val2
    # val1 -ge val2 Returns true if val1 is greater than or equal to val2
    # val1 -lt val2 Returns true if val1 is less than val2
    # val1 -le val2 Returns true if val1 is less than or equal to val2

    ## FILE CONDITIONALS
    # FILE="test.txt"

    # if [ -f "$FILE" ]
    # then
    # echo "$FILE is a file"
    # else
    # echo "$FILE is NOT a file"
    # fi

    # -d file True if the file is a directory
    # -e file True if the file exists (note that this is not particularly portable, thus -f is generally used)
    # -f file True if the provided string is a file
    # -g file True if the group id is set on a file
    # -r file True if the file is readable
    # -s file True if the file has a non-zero size
    # -u True if the user id is set on a file
    # -w True if the file is writable
    # -x True if the file is an executable


    ## FOR LOOP
    # NAMES="Brad John Harry Karen Sam"
    # for NAME in $NAMES
    # do
    # echo "Hello ${NAME}"
    # done

    ## RENAME A BUNCH OF FILES USING A FOR LOOP
    # FILES=$(ls *txt)
    # NEW="new"
    # for FILE in $FILES
    # do
    # echo "Renaming $FILE to new-$FILE"
    # mv $FILE $NEW-$FILE
    # done

    ## WHILE LOOP - Read through a file line by line
    # LINE=1
    # while read -r CURRENT_LINE
    # do
    # echo "${LINE}: $CURRENT_LINE"
    # ((LINE++))
    # done < "./new-test.txt"


    ## FUNCTION
    # function sayHello () {
    # echo "Hello World"
    # }

    # sayHello

    ## FUNCTION WITH PARAMS
    # function greet() {
    # echo "Hello, I am $1 and I am $2"
    # }

    # greet "Brad" "36"


    # CREATE AND WRITE TO A FILE
    # mkdir test
    # touch "test/hello.txt"
    # echo "Hello World" >> "test/hello.txt"
    # echo "Created test/hello.txt"