Last active
July 23, 2023 11:38
-
-
Save aneurysmjs/6ad954538524731efe323b35e4c93767 to your computer and use it in GitHub Desktop.
some basic bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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