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
(Mediawiki's links are made with square brackets and that messes up the if statements)
Line 68: Line 68:


=== If, Elif, Else ===
=== If, Elif, Else ===
<pre>
#!/bin/bash


echo "Enter a number:"
#!/bin/bash
read number

echo "Enter a number:"
# if and elif both have a "then" associated with it
read number
if [ $number -eq 1 ]
then
# if and elif both have a "then" associated with it
echo 1
if [ $number -eq 1 ]
elif [[ $number -eq 2 ]]
then
then
echo 1
echo 2
elif [[ $number -eq 2 ]]
else
then
echo "You can't get anything right you hopeless loser"
echo 2
fi
else
</pre>
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).

Revision as of 14:54, 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 !=.

if [ "$foo" == "$bar"]
# or
if [ "$foo" != "$bar"]


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"'