Bash: Difference between revisions

From MTU LUG Wiki
Jump to navigation Jump to search
imported>Sjwhitak
(Moved messed up link to real link.)
 
imported>Sjwhitak
(Wooo syntax highlighting is awesome)
 
(One intermediate revision by the same user not shown)
Line 13: Line 13:
Variables are super simple, here is a basic example of a hello world program bash file.
Variables are super simple, here is a basic example of a hello world program bash file.


<syntaxhighlight lang="bash">
#!/bin/bash
#!/bin/bash

greeting="You are a warm popsicle"
greeting="You are a warm popsicle"
echo $greeting
echo $greeting
</syntaxhighlight>


When creating and assigning a value to a variable, make sure to include no spaces between the variable name, the equal sign, and the value of the variable. Then when you want to call a variable for use, like the greeting, we use a dollar sign to indicate that it is a variable. Note, variables like $1, $0, $11 are reserved for command line arguments so avoid using them.
When creating and assigning a value to a variable, make sure to include no spaces between the variable name, the equal sign, and the value of the variable. Then when you want to call a variable for use, like the greeting, we use a dollar sign to indicate that it is a variable. Note, variables like $1, $0, $11 are reserved for command line arguments so avoid using them.
Line 22: Line 24:
=== Input ===
=== Input ===
Read values inline:
Read values inline:
<syntaxhighlight lang="bash">
#!/bin/bash


echo "Guess a number"
#!/bin/bash
# Use read to create the number variable and
# immediately set the variable to the input
echo "Guess a number"
# Use read to create the number variable and
read number
echo "You get slapped $number times."
# immediately set the variable to the input
</syntaxhighlight>
read number
echo "You get slapped $number times."


Read parameters from command-line:
Read parameters from command-line:
<syntaxhighlight lang="bash">
#!/bin/bash


# Prints the name of the file
#!/bin/bash
echo $0
# Prints the name of the file
# Prints the first parameter passed to the file
echo $0
echo $1
# Prints the first parameter passed to the file
echo $1
# $@ is used to represent all of the command line inputs
# Here we are just iterating through all command line inputs
for param in "$@"
do
echo $param
done


# $@ is used to represent all of the command line inputs
# Here we are just iterating through all command line inputs
for param in "$@"
do
echo $param
done
</syntaxhighlight>


=== Operators ===
=== Operators ===
Line 61: Line 64:


As a side note, when comparing two strings, always put strings or variables referencing strings in parenthesis and compare using == or !=.
As a side note, when comparing two strings, always put strings or variables referencing strings in parenthesis and compare using == or !=.
syntaxhighlight lang="bash">
if [ "$foo" == "$bar"]
# or
if [ "$foo" != "$bar"]
</syntaxhighlight>


=== If, Elif, Else ===
if [ "$foo" == "$bar"]
<syntaxhighlight lang="bash">
# or
#!/bin/bash
if [ "$foo" != "$bar"]


echo "Enter a number:"
read number


# if and elif both have a "then" associated with it
=== If, Elif, Else ===
if [ $number -eq 1 ]

then
#!/bin/bash
echo 1
echo "Enter a number:"
elif [[ $number -eq 2 ]]
then
read number
echo 2
else
# if and elif both have a "then" associated with it
echo "You can't get anything right you hopeless loser"
if [ $number -eq 1 ]
fi
then
</syntaxhighlight>
echo 1
elif [[ $number -eq 2 ]]
then
echo 2
else
echo "You can't get anything right you hopeless loser"
fi


This is a simple example that just reads in a number and checks to see if the number is either 1, 2, or, if neither of these are true, will just insult you. The syntax here is pretty simple. You have the if statement which uses square brackets for the conditional and elif statements which use double square brackets. There is also the default else statement and the whole statement is closed off by a fi (if backwards).
This is a simple example that just reads in a number and checks to see if the number is either 1, 2, or, if neither of these are true, will just insult you. The syntax here is pretty simple. You have the if statement which uses square brackets for the conditional and elif statements which use double square brackets. There is also the default else statement and the whole statement is closed off by a fi (if backwards).
Line 91: Line 95:


For:
For:
<syntaxhighlight lang="bash">
#!/bin/bash


# Iterates through all objects in an array, all inputsguessed, etc.
#!/bin/bash
for word in $paragraph
do
# Iterates through all objects in an array, all inputsguessed, etc.
echo $word
for word in $paragraph
done
do
</syntaxhighlight>
echo $word
done

While:
While:
<syntaxhighlight lang="bash">
#!/bin/bash


# Runs while the conditional is being met
#!/bin/bash
while [ $index -lt 5 ]
do
# Runs while the conditional is being met
echo $index
while [ $index -lt 5 ]
# Incrementing values is obnoxious
do
echo $index
index=$((index + 1))
done
# Incrementing values is obnoxious
</syntaxhighlight>
index=$((index + 1))
done

Until:
Until:
<syntaxhighlight lang="bash">
#!/bin/bash


# Runs until the conditional is met
#!/bin/bash
until [ $index -eq 5 ]
do
# Runs until the conditional is met
echo $index
until [ $index -eq 5 ]
# Still obnoxious
do
echo $index
index=$((index + 1))
done
# Still obnoxious
</syntaxhighlight>
index=$((index + 1))
done

=== Iterating through files in a directory ===
=== Iterating through files in a directory ===
<syntaxhighlight lang="bash">
#!/bin/bash


# Path to the directory we want to check out
#!/bin/bash
files=~/Downloads/*
# Path to the directory we want to check out
# The for loop is back baby
files=~/Downloads/*
for file in $files
do
# The for loop is back baby
echo $file
for file in $files
# Prints out the absolute path of each file
do
done
echo $file
</syntaxhighlight>
# Prints out the absolute path of each file
done

Bash is able to automatically detect whether a variable represents a directory or not. It's magic and is definitely not regex.
Bash is able to automatically detect whether a variable represents a directory or not. It's magic and is definitely not regex.


=== Aliases ===
=== Aliases ===
You can use aliases to create cli commands from bash files. Aliases can be set in your ~/.bashrc file. Make sure to make your bash file executable and move it to your
You can use aliases to create cli commands from bash files. Aliases can be set in your ~/.bashrc file. Make sure to make your bash file executable and move it to your
<syntaxhighlight lang="bash">

alias epiccommand='./epiccommand.sh'
alias epiccommand='./epiccommand.sh'
alias epiccommand='./epiccommand.sh "Nerds"'
alias epiccommand='./epiccommand.sh "Nerds"'
</syntaxhighlight>

Latest revision as of 16:14, 14 March 2022

Basic Bash

It was too much effort to make a full guide so this is just the basics.

What is bash?

It is literally just a file full of commands. It allows users to write automation scripts because us sweaty nerds are the laziest human beings the human race has to offer. Typing commands over and over again is hard work and requires a mild amount of effort.

Creating a bash file

Bash files use the .sh file extension. They have two basic things they need in order to run. They need a shebang line and to be made executable.

  • The shebang line just goes at the beginning of the file and tells your computer what to do with the file. For bash files the shebang line is just "#!/bin/bash"
  • To make the file executable, just use the classic "chmod +x FILE_NAME"

Variables

Variables are super simple, here is a basic example of a hello world program bash file.

#!/bin/bash

greeting="You are a warm popsicle"
echo $greeting

When creating and assigning a value to a variable, make sure to include no spaces between the variable name, the equal sign, and the value of the variable. Then when you want to call a variable for use, like the greeting, we use a dollar sign to indicate that it is a variable. Note, variables like $1, $0, $11 are reserved for command line arguments so avoid using them.

Input

Read values inline:

#!/bin/bash

echo "Guess a number" 
# Use read to create the number variable and
# immediately set the variable to the input
read number
echo "You get slapped $number times."

Read parameters from command-line:

#!/bin/bash

# Prints the name of the file
echo $0
# Prints the first parameter passed to the file
echo $1

# $@ is used to represent all of the command line inputs
# Here we are just iterating through all command line inputs
for param in "$@"
do
     echo $param
done

Operators

The operators for bash are:

  • Equal: -eq
  • Not equal: -ne
  • Less than or equal: -le
  • Less than: -lt
  • Greater than or equal: -ge
  • Greater than: -gt
  • Is null: -z
  • or: ||
  • and: &&

As a side note, when comparing two strings, always put strings or variables referencing strings in parenthesis and compare using == or !=. syntaxhighlight lang="bash"> if [ "$foo" == "$bar"]

  1. or

if [ "$foo" != "$bar"] </syntaxhighlight>

If, Elif, Else

#!/bin/bash

echo "Enter a number:"
read number

# if and elif both have a "then" associated with it
if [ $number -eq 1 ]
then
     echo 1
elif [[ $number -eq 2 ]]
then
     echo 2
else
     echo "You can't get anything right you hopeless loser"
fi

This is a simple example that just reads in a number and checks to see if the number is either 1, 2, or, if neither of these are true, will just insult you. The syntax here is pretty simple. You have the if statement which uses square brackets for the conditional and elif statements which use double square brackets. There is also the default else statement and the whole statement is closed off by a fi (if backwards).

Loops

There are three types of loops:

For:

#!/bin/bash

# Iterates through all objects in an array, all inputsguessed, etc.
for word in $paragraph
do
     echo $word
done

While:

#!/bin/bash

# Runs while the conditional is being met
while [ $index -lt 5 ]
do
     echo $index
     # Incrementing values is obnoxious
     index=$((index + 1))
done

Until:

#!/bin/bash

# Runs until the conditional is met
until [ $index -eq 5 ]
do
     echo $index
     # Still obnoxious
     index=$((index + 1))
done

Iterating through files in a directory

#!/bin/bash

# Path to the directory we want to check out
files=~/Downloads/*
    
# The for loop is back baby
for file in $files
do
     echo $file
     # Prints out the absolute path of each file
done

Bash is able to automatically detect whether a variable represents a directory or not. It's magic and is definitely not regex.

Aliases

You can use aliases to create cli commands from bash files. Aliases can be set in your ~/.bashrc file. Make sure to make your bash file executable and move it to your

alias epiccommand='./epiccommand.sh'
alias epiccommand='./epiccommand.sh "Nerds"'