Skip to content

Instantly share code, notes, and snippets.

@aneurysmjs
Last active July 23, 2023 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aneurysmjs/6ad954538524731efe323b35e4c93767 to your computer and use it in GitHub Desktop.
Save aneurysmjs/6ad954538524731efe323b35e4c93767 to your computer and use it in GitHub Desktop.
some basic bash
num=10
# -eq equals
# -ne not equal
# -gt greather than
# -ge greather than or equal
# -lt less than
# -le less than or equal
if [ $num -eq 10]
then
echo "condition is true"
else
echo "condition is false"
fi # always close the if statement
## check the prescence of a file
# -f for file
# -d for directory
if [ -f ~/some-file ]
then
echo "file exists"
else
echo "file does not exist"
fi
#!/bin/bash
# which - checks whether a binary, command is present on the file system
command=/usr/local/bin/htop # fully qualified path
if [ -f $command]
then
echo "$command is available, running it"
else
echo "fuck you, installing it..."
# apt update (Debian, Ubuntu) is not what it sounds, it does not update packages, instead it synchronize with the mirror, the repository
# to find out what packages are actually available
# -y means assume yes, don't give me a confirmation prompt
# sudo apt update && sudo apt install -y htop # on linux
brew install htop # on macOS
fi
$command # run the command
## check command's existence dynamically
theCommand=htop
# note we're not using brackets, brackets are only needed when running the `test` command, even thou we didn't type out the `test` command
# when using [] in a if statement we're using the `test` command
if command -v $theCommand
# `command -v` checks the existence of a command
then
echo "$theCommand is available, running it"
else
echo "fuck you, installing it..."
brew install $theCommand # on macOS
fi
#!/bin/bash
# To detect whether a bash script is running on Linux or macOS, you can check the value of the $OSTYPE environment variable.
# The $OSTYPE variable contains information about the operating system type.
# On Linux, it typically has a value like "linux-gnu," and on macOS, it usually has a value like "darwin."
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo "Running on Linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo "Running on macOS"
else
echo "Unsupported operating system"
fi
#!/bin/bash
# the shebang is the very first line in any script that tells the script which interpreter is suppose to use
## commands
# print to the console
echo "que pasa papi"
echo "my current directoryis: "
pwd
#!/bin/bash
## variables
# to create a variable just simply write the name following the = sign with no empty space in between
name_of_var="value"
# whenever we reference a variable in bash, we have to include the $ sign in front of the name of the variable
echo $name_of_var
ls="yeah"
# if you type ls, bash knows that is a command because it doesn't have the $ in front of it
echo $ls # "yeah"
age=34
echo "I'm $age years old"
## capture the output of a command
# so you make a variable that's equal to the output of a command
files=$(ls) # we use parentesis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment