Skip to content

Instantly share code, notes, and snippets.

@stanleycyang
Created May 30, 2015 20:42
Show Gist options
  • Save stanleycyang/ef80bd706960123a7e07 to your computer and use it in GitHub Desktop.
Save stanleycyang/ef80bd706960123a7e07 to your computer and use it in GitHub Desktop.

#Writing Bash Shell Scripts

###Objectives

  • Understand what bash scripts are
  • Declare our first variable
  • Write our first bash script

###What is a shell script?

A shell script is a little more than a list of command that are run in sequence. By convention, a shell script should begin with

#! /bin/bash

###Writing our first bash script

#! /bin/bash

# Declare a variable
cool='cool'	

echo "Hello $USER. You are $cool"

In bash, you can use the environmental variables set inside of your computer (See them using printenv

####Local variables

You can declare local variables by:

cool='cool'

Make sure there are no spaces in between!!

In this example, quotes are optional because there is one word. If there are spaces, then quotes are not optional

To call on it, use $:

echo "You are $cool"

Notice in bash, single quotes and double quotes matter. Variables can be called in double quotes but not single quotes

You can escape a variable using \

###Finishing touches on first shell script

#! /bin/bash

cool='cool'

echo "Hello $USER. I will list some files."
echo "Listing files in current directory"
ls
echo -n "You are $cool"
echo ' bro'

###Double quotes are powerful

LS="ls"
LS_FLAGS="-al"

$LS $LS_FLAGS $HOME # shows everything in home ls -al /home/stanley

###When combining variables with string

x='ABC'
echo "${x}abc"

###Conditionals

It follows in this syntax

if condition1
then
	statement1
	statement2
	..........
elif condition2
then
	statement3
	statement4
	........    
elif condition3
then
	statement5
	statement6
	........    


fi

###Simple if statement

if [ $X -lt $Y ] # if X is less than
Y
then
  echo "\$X=${X} is smaller than \$Y=${Y}"
fi

###Simple if else statement

empty_string=""

if [ -n "$empty_string" ] # -n tests to see if the argument is non empty
then
  echo "\$empty_string is nonempty"
else
  echo "\$empty_string is empty"
fi

###Check if a file exists

Next, let's create a .stanley file in the current directory

Check if .stanley exists

file="./.stanley"

if [ -e "$file" ]; then
  echo "You have a .stanley file"
  if [ -L "$file" ]; then # -L checks if its a symlink
    echo "It's a symlink"
  elif [ -f "$file" ]; then # file checks if its a regular file
    echo "It's a regular file"
  fi
else
	echo ".stanley does not exist"
fi

###Common operators

###Kicks and Giggles

#! /bin/bash
curl https://www.google.com -o output.txt

This will curl all your outputs from google into a file

###Grabbing all the files

echo *
echo ${HOME}/*

###Declaring an array

ARRAY=(one two three four five)
ARRAY[5]=blue
echo ${ARRAY[*]}

#Delete a value
unset ARRAY[1]
echo ${ARRAY[*]}

###For loops

####Basic for loop

for X in red green blue
do
  echo $X
done

###With an array

ARRAY=(one two three four)
#echo ${ARRAY[*]}
#ARRAY[5]=blue
#unset ARRAY[1]
#echo ${ARRAY[*]}

for X in ${ARRAY[*]}
do
  echo $X
done	

This raises a natural question: why doesn't bash allow the C like for loops

for (X=1,X<10; X++) As it happens, this is discouraged for a reason: bash is an interpreted language, and a rather slow one for that matter. For this reason, heavy iteration is discouraged.

###While loops

x=0

while [ $x -le 20 ]
do
  echo $x
  x=$((x+1))
done

###Creating the Stanley Framework!

#! /bin/bash

if [ -z $1 ]; then
  APP='STANLEY'
else
  APP=$1
fi

# Create my app structure
mkdir $APP $APP/js $APP/css
# echo out results
echo "Created $APP"
echo "Created ${APP}/css"
echo "Created ${APP}/js"

# Constants
TITLE="${USER}'s badass framework"
RIGHT_NOW=$(date +"%x %r %Z")


# Call the functions
cat > $APP/index.html <<- EOM
  <html>
    <head>
      <title>$TITLE</title>
      <link href='css/style.css'></link>
    </head>
    <body>
      <h1>$TITLE</h1>
      <p>Created $RIGHT_NOW</p>
      <script src='js/main.js'></script>
    </body>
  </html>
EOM

# Alert that index.html was created
echo "Created ${APP}/index.html"

cat > $APP/css/style.css <<- EOM

  body{
    padding: 0;
    margin: 0;
  }

EOM

echo "Created ${APP}/css/style.css"

cat > $APP/js/main.js <<- EOM

  console.log('activate framework!');

EOM

echo "Created ${APP}/js/main.js"

Finally, to use it anywhere

$ sudo mv stan /bin

###Congrats! You have written a useful bash script!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment