docs
  • Overview
  • 🐍 PYTHON
    • Type Hints
    • PEP8 Style Guide for Python Code
    • 🏡Pipenv
    • Pathlib
  • 🕸Django
    • 🗄models
      • 🎯Best Practices
      • 🚦Django Signals
    • ⚙️ settings
    • DRF
      • Serializer
      • Authentication
      • Permissions
      • Viewsets
    • Testing
      • Faker and Factory Boy
    • 🧪Test Coverage
    • 💦Python-Decouple
    • Django Tips:
    • 💾Django ORM Queryset
    • Custom Exceptions
    • Celery
    • Resources
  • Deploy
    • 🚀Django Deployment
    • 🔒Setup SSL Certificate
  • 💾Database
    • MongoDB
  • 🛠️DevOps
    • 🖥Scripting
      • A First Script
      • Loops
      • Test
      • Variables
      • External programs
      • Functions
    • Command Line Shortcuts
    • Basic Linux Commands
    • 🎛Microservices
    • 🐳Docker
      • Docker Commands
      • Docker Compose
      • Django project
    • Kubernates
  • 📝Software IDE
    • EditorConfig
    • Linters
    • VsCode
Powered by GitBook
On this page
  • Variables - Part I
  • Wildcards:
  • Escape Characters:

Was this helpful?

  1. DevOps
  2. 🖥Scripting

A First Script

Shell script are powerful tools for DevOps

#!/bin/sh
# This is a comment!
echo Hello World        # This is a comment, too!

The first line tells Unix that the file is to be executed by /bin/sh. The # symbol still marks a comment; the # and anything following it is ignored by the shell. The file starts with #!is a special directive which Unix treats specially #!/usr/bin/python to tell your interactive shell that the program which follows should be executed by python.

Now run chmod 755 first.sh to make the text file executable, and run ./first.sh. Your screen should then look like this:

$ chmod 755 first.sh
$ ./first.sh
Hello World
$

Variables - Part I

variables - a symbolic name for a chunk of memory to which we can assign values, read and manipulate its contents. Note: There must be no spaces around the "=" sign: VAR=value works; VAR = value doesn't work. In the first case, the shell sees the "=" symbol and treats the command as a variable assignment. In the second case, the shell assumes that VAR must be the name of a command and tries to execute it.

#!/bin/sh
MESSAGE="Hello World"
echo $MESSAGE

variables may store strings, integers, real numbers. In truth, these are all stored as strings We can interactively set variable names using the read command. shell-builtin command read which reads a line from standard input into the variable supplied

#!/bin/sh
echo What is your name?
read NAME
echo "Hello $NAME - hope you're well.

There is a command called export which has a fundamental effect on the scope of variables.

#!/bin/sh
echo "VARIABLE is: $VAR"
VAR="Hello World"
echo "VARIABLE is: $VAR"

Now run the script:

$ ./var2.sh
VARIABLE is:
VARIABLE is: Hello World

Run again.

$ VAR=Welcome
$ ./var2.sh
VAR is:
VAR is: Hello World

VAR hasn't been set to any value, so it's blank.

We need to export the variable for it to be inherited by another program.

$ export VAR
$ ./var2.sh
VAR is: Welcome
VAR is: Hello World

This is changing the value of VAR

$ echo $VAR
Welcome
$

Once the shell script exits, its environment is destroyed. But VAR keeps its value of Welcome within your interactive shell. In order to receive environment changes back from the script, we can source a script via the "." (dot) command:

$ VAR=Welcome
$ echo $VAR
Welcome
$ . ./var2.sh
VAR is: Welcome
VAR is: Hello World
$ echo $VAR
Hello World

The change has now made it out into our shell again! This is how your .profile or .bash_profile file works.

we enclose the variable itself in curly { } brackets:

#!/bin/sh
echo "What is your name?"
read NAME
echo "Hello $NAME"
echo "create a file called ${NAME}_file"
touch "${NAME}_file"

Wildcards:

Think first how you would copy all the files from /folder/src into /folder/apps. All the .txt files? All the .html files? Hopefully you will have come up with:

$ cp /folder/src/* /folder/apps/
$ cp /folder/src/*.txt /folder/apps/
$ cp /folder/src/*.html /folder/apps/

Escape Characters:

The use of double quotes (") characters affect how spaces and TAB characters are treated

$ echo Hello       World
Hello World
$ echo "Hello       World"
Hello     World

So how do we display: Hello "World" ?

$ echo "Hello   \"World\""

Most characters (*, ', etc) are not interpreted (ie, they are taken literally) by means of placing them in double quotes (" ").

$ echo *
case.shtml escape.shtml first.shtml 
functions.shtml hints.shtml index.shtml 
ip-primer.txt raid1+0.txt
$ echo *txt
ip-primer.txt raid1+0.txt
$ echo "*"
*
$ echo "*txt"
*txt

In the first example, * is expanded to mean all files in the current directory. In the second example, *txt means all files ending in txt. In the third, we put the * in double quotes, and it is interpreted literally. In the fourth example, the same applies, but we have appended txt to the string. However, ", $, `, and \ are still interpreted by the shell, even when they're in double quotes. The backslash \ character is used to mark these special characters so that they are not interpreted by the shell.

$ echo "This is \\ a backslash"
This is \ a backslash
$ echo "This is \" a quote and this is \\ a backslash"
This is " a quote and this is \ a backslash
Previous🖥ScriptingNextLoops

Last updated 5 years ago

Was this helpful?

🛠️