Skip to content

Instantly share code, notes, and snippets.

@bradtraversy
Last active May 26, 2024 20:35
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.
Basic Shell Scripting
#! /bin/bash
# ECHO COMMAND
# echo Hello World!
# VARIABLES
# Uppercase by convention
# Letters, numbers, underscores
NAME="Bob"
# echo "My name is $NAME"
# echo "My name is ${NAME}"
# USER INPUT
# read -p "Enter your name: " NAME
# echo "Hello $NAME, nice to meet you!"
# 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 or Jack"
# fi
# 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 CONDITIONS
# FILE="test.txt"
# if [ -e "$FILE" ]
# then
# 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
########
#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"
# done
# 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
# 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-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"
@NTaylor23
Copy link

Good stuff

@sauravpatar
Copy link

Thank You

@Monmoy042
Copy link

Thanks man. It helps a lot

@themaleem
Copy link

themaleem commented Dec 26, 2021

Helped a lot, ty Brad!

@musta55
Copy link

musta55 commented Jan 1, 2022

Helped a lot brad!

@ilosrim
Copy link

ilosrim commented Feb 7, 2022

echo "Thanks man"

@AdewolePhoenix
Copy link

Thank you so much for this.

@Nikhil-999
Copy link

Thanks

@hariharandev2020
Copy link

hariharandev2020 commented Mar 1, 2022

Thanks a lot man..!!!!

@MujorB
Copy link

MujorB commented Mar 2, 2022

Thanks man, you did a great job here..

@GrubBox
Copy link

GrubBox commented Mar 2, 2022

Man is a legend !

@akhan4u
Copy link

akhan4u commented Apr 3, 2022

Loved it. Especially the gist helps a lot!

@trojan782
Copy link

Excellent stuff Here!

@balajisomasale
Copy link

Thanks!

@InfuriousD
Copy link

Man Love it thanks, I knew other programming languages and you explained the rest of it well UwU!

@CamiloSinningUN
Copy link

Thanks

@alphaoumardev
Copy link

It helps me thanks

@Yuqiwu
Copy link

Yuqiwu commented Jun 9, 2022

Thank you!

@cliqueengagements
Copy link

Do you offer classes

@Shane-Shen
Copy link

It’s really helpful. Thanks

@HRosete
Copy link

HRosete commented Sep 14, 2022

Thank you! 🙏

@Kenobinna
Copy link

Thank you Brad

@edwalters99
Copy link

Thanks so much - really useful

@iamRichardCN
Copy link

thank you

@shaungt1
Copy link

Thank you very awesome, you ever plan on doing one that can interact with different files like python and JavaScript on a server that would be cool.

@simozgaoua
Copy link

thanks

@Nonijay-star
Copy link

thank you!

@mmarach
Copy link

mmarach commented Sep 15, 2023

Thanks, bud!

@deluxesande
Copy link

Thank you

@alphaoumardev
Copy link

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment