16
Using bash Administrative Shell Scripting COMP2101 Fall 2017

COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Embed Size (px)

Citation preview

Page 1: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Using bashAdministrative Shell Scripting

COMP2101 Fall 2017

Page 2: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Bash Background

• Bash was written to replace the Bourne shell

• The Bourne shell (sh) was not a good candidate for rewrite, so bash was a completely new program

• Bash was produced in the late 1980s implementing the design philosophy and command structures of existing shells

• Execution is structured and data is viewed as a stream of bytes although modern versions support array variables

• Bash is open source and actively maintained

Page 3: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Shell Process• The bash shell is just a command ( /bin/bash )

• It runs like any other command, as a process

• Bash is single-threaded in nature and does one thing at a time

• Processes are started by other processes - they fork

• Processes inherit an environment from the process that started them - the new process is called the child process and the original process is called the parent process

• Programs often look for environment ( sometimes called startup ) files to define or override their inherited operating environment and provide tools to the program user

Page 4: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Login Shell• Bash is commonly used as Linux shell for command

line interaction with the operating system

• It is used to start other programs, manage files, and observe and control the system

• The first shell started when you log onto a Linux system is your login shell, subsequent shells such as in terminal windows if you use a gui, are interactive non-login shells

• When your login shell process ends, you are logged out

Page 5: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

bash Environment Files• bash runs /etc/profile before running user-specific environment script files

• bash looks for ~/.bash_profile, ~/.bash_login, ~/.profile and ~/.bash_logout for login shells

• bash looks for ~/.bashrc for interactive non-login shells

• A login shell is defined as one created by the login program (the login program is the one that asks you for your password), or another program that might manage your login, such as sshd.

• Environment files are shell scripts that run commands to configure programs, set variables, manipulate files, and/or send messages

• Each user can customize their own environment files

Page 6: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Exercises

• Use the ps command with grep to identify what bash shell processes are running in your system currently, and identify what program started each one

• Examine the environment files present by default on your Linux system

• Add an echo command to your existing environment files that tells you they ran (use echo), then start another login and another child shell to see which ones get used

Page 7: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Script Content• Scripts can contain commands,

blank space, comments, and inline data

• Scripts are a minimum of one line, with no practical limits on length

• Commands in scripts are the exact same commands you could use on the command line interactively

• Scripts end when they encounter a fatal bash error, or the exit command, or run out of commands

#!/bin/bash # My first script

echo 'Hello World!' echo "I am process # $$"

#!/bin/bash # My second script

cat <<EOF Hello World! I am process # $$ EOF

Page 8: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Script Creation/Modification• Scripts are text files

• They can be created and edited the same as any other text file

• This is usually done with a text editor such as nano or vi or atom

• Many IDEs know bash and can be helpful

• github.com, c9.io, koding.com, etc. are useful web-based developer support sites

vi scriptfile.sh

nano scriptfile.sh

double-click file in a file manager

right-click file in a file manager and choose an editing environment

Page 9: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Exercises

• Create the 2 hello world scripts from the previous slides

• Make sure they run without error

bash scriptfilename

Page 10: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Script Structure• Linux scripts are free-form with one exception • The first line

• identifies the script as a script (magic number #!) • specifies the command interpreter to use along with any options

• The remainder of the script can be anything valid for the interpreter • The exit command can be used to force a script to immediately end

#!/bin/bash

#!/bin/bash -x

#!/usr/local/bin/perl

#!/path/to/interpreter –option1 –option2 …

Page 11: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Comments

• A comment is any text beginning with #

• They provide the reader of the script with useful information

• They can also be used as part of the process of debugging scripts

# This is a comment

# Comments are ignored by the interpreter

echo “Hello World” # this is a comment on the same line as a command

# funky-command-that-might-be-causing-trouble

Page 12: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Common Comment Use• It can be very helpful to put some comments at the start of a

script describing the script’s purpose(s), inputs, and outputs

• Use comments to explain uncommon or difficult to read commands

# helloscript.sh # This script displays the string “Hello World!”

# This is a silly way of creating the output text by starting with something else and stream editing it in a pipeline echo -n "helb wold" |sed -e "s/b/o/g" -e "s/l/ll/" -e "s/ol/orl/" |tr "h" "H"|tr "w" "W"|awk '{print $1 "\x20" $2 "\41"}'

Page 13: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Exercises

• Create the obfuscated hello world script from the previous slide - be careful to create it exactly as shown on the slide

• Make sure it runs without error

• Try to determine how it does what it does and add comments to explain how it works

• Try running your scripts with the -x option to bash

Page 14: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Script Execution• Scripts can be run as commands or by specifying them as

an argument to the bash command

• Either way, they run in a new child process, not the current shell process, so they inherit your environment but do not have access to your local variables

• Running a script as a command requires execute permission for the script file and that the shell can find the script file

• To execute a script file in the current shell process instead of a child process, source it using the source command or its alias, the . (dot) command - this is equivalent to copying and pasting the script into the current shell

• Scripts can be copied and pasted onto a bash command line if you want them to run in the current process, be careful if you try to do this between Windows and any other operating system

bash scriptfile.sh

chmod u+x scriptfile.sh ./scriptfile.sh

mv scriptfile.sh ~/bin scriptfile.sh

source ~/bin/scriptfile.sh . ~/bin/scriptfile.sh

Page 15: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Script Storage• In order to run a script, the shell must be able to find the script file

• bash uses the PATH variable to locate commands which are not built-in

• Scripts are often stored in a directory associated with their purpose, personal scripts are often stored in ~/bin

• Your script storage directory can be added to your shell command path - be sure to add this to your environment files if you want it to be there every time you login

export PATH=$PATH:/path/to/scripts

Page 16: COMP2101 Bash 01 - Introduction - zonzorp.github.io Bash 01... · Login Shell • Bash is commonly used as Linux shell for command line interaction with the operating system • It

Exercises

• Create a bin directory in your home directory and copy or move your script files to that directory

• Add your personal bin directory to your command path if it isn't already there

• Source the environment file to make sure it works properly if you changed it

• Verify you can run your scripts without typing a path to them anymore