Bash: Difference between revisions
Jump to navigation
Jump to search
Wooo syntax highlighting is awesome
imported>Sjwhitak (Mediawiki's links are made with square brackets and that messes up the if statements) |
imported>Sjwhitak (Wooo syntax highlighting is awesome) |
||
Variables are super simple, here is a basic example of a hello world program bash file.
<syntaxhighlight lang="bash">
#!/bin/bash
greeting="You are a warm popsicle"
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.
=== Input ===
Read values inline:
<syntaxhighlight lang="bash">
#!/bin/bash
echo "Guess a number"
# Use read to create the number variable and
# immediately set the variable to the input
echo "You get slapped $number times."
</syntaxhighlight>
Read parameters from command-line:
<syntaxhighlight lang="bash">
#!/bin/bash
# Prints the name of the file
echo $0
# $@ 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 ===
As a side note, when comparing two strings, always put strings or variables referencing strings in parenthesis and compare using == or !=.
syntaxhighlight lang="bash">
</syntaxhighlight>
=== If, Elif, Else ===
<syntaxhighlight lang="bash">
#!/bin/bash
echo "You can't get anything right you hopeless loser"
fi
</syntaxhighlight>
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).
For:
<syntaxhighlight lang="bash">
#!/bin/bash
# Iterates through all objects in an array, all inputsguessed, etc.
for word in $paragraph
do
echo $word
done
</syntaxhighlight>
While:
<syntaxhighlight lang="bash">
#!/bin/bash
# Runs while the conditional is being met
while [ $index -lt 5 ]
do
echo $index
# Incrementing values is obnoxious
done
</syntaxhighlight>
Until:
<syntaxhighlight lang="bash">
#!/bin/bash
# Runs until the conditional is met
until [ $index -eq 5 ]
do
echo $index
# Still obnoxious
done
</syntaxhighlight>
=== Iterating through files in a directory ===
<syntaxhighlight lang="bash">
#!/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
</syntaxhighlight>
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
<syntaxhighlight lang="bash">
</syntaxhighlight>
|