Skip to content

Instantly share code, notes, and snippets.

@cliqueengagements
Forked from bradtraversy/myscript.sh
Last active July 17, 2022 18:03
Show Gist options
  • Save cliqueengagements/fb1ca8fc6925e4af262badf34d18f1ee to your computer and use it in GitHub Desktop.
Save cliqueengagements/fb1ca8fc6925e4af262badf34d18f1ee to your computer and use it in GitHub Desktop.
Basic Shell Scripting
#! /usr/bin/bash
# echo Hello World!!
# sleep 3
# VARIABLE
# uPPERCASE BY CONVENTIONS
# LETTERS NUMBERS UNDERSCORES
# NAME="Bob"
# echo "my name is $NAME"
# User Input
# read -p "Enter your name: " NAME
# echo "Hello $NAME, nice to meet you"
# If statement
# if [ "$NAME" == "Sam" ]
# then
# echo "Your name is Sam"
# fi
# If-Else statement
# if [ "$NAME" == "Sam" ]
# then
# echo "Your name is Sam"
# else
# echo Searching....
# sleep 3
# echo "Your name is not registered"
# fi
# ELSE-IF Statement
# if [ "$NAME" == "Sam" ]
# then
# echo Searching...
# sleep 3
# echo "Your name is Sam"
# elif [ "$NAME" == "Jack" ]
# then
# echo Searching...
# sleep 3
# echo "Your name is Jack"
# else
# echo Searching....
# sleep 3
# echo "Your name is not registered"
# fi
#COMPARISIONS
# NUM1=32
# NUM2=5
# if [ "$NUM1" -gt "$NUM2" ]
# then
# echo "$NUM1 is greater than $NUM2"
# else
# echo "$NUM1 is less than $NUM2"
# fi
########
# val1 -eq (equal) val2 Returns true if the values are equal
# val1 -ne (not eq) val2 Returns true if the values are not equal
# val1 -gt (grt than) val2 Returns true if val1 is greater than val2
# val1 -ge (grt than or eq) val2 Returns true if val1 is greater than or equal to val2
# val1 -lt (less than) val2 Returns true if val1 is less than val2
# val1 -le (less than or eq) val2 Returns true if val1 is less than or equal to val2
########
#FILE CONDITIONS
# FILE="hello.txt"
# if [ -f "$FILE" ]
# then
# echo Searching Directory....
# sleep 2
# echo "$FILE exists"
# else
# echo Searching Directory....
# sleep 2
# 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
########
#Case Statements
# read -p "Are you 21 or above? Y/N " ANSWER
# case "$ANSWER" in
# [y/Y] | [y/Y][e/E][s/S])
# echo "Veryfing..."
# sleep 2
# echo "You can have a beer :)"
# ;;
# [n/N] | [n/N][o/O])
# echo "Veryfing..."
# sleep 2
# echo "You can't have a beer :("
# ;;
# *) #incase user inputs anything other than required answer
# echo "Please enter y/yes or n/no"
# esac
# #Simple for loop
# NAMES="Jack Sam Temi Mike"
# for NAME in $NAMES
# do
# echo "Hello $NAME"
# done
#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 Reading through lines
# LINE=1
# while read -r CURRENT_LINE
# do
# echo "$LINE: $CURRENT_LINE"
# ((LINE++))
# done < "./new-1.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 FOLDER AND WRITE TO A FILE
# mkdir hello
# touch "hello/world.txt"
# echo "Hello World" >> "hello/world.txt"
# echo "Created hello/world.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment