BASH shell en: Difference between revisions

From ICO wiki
Jump to navigationJump to search
Line 47: Line 47:
<source lang="bash">
<source lang="bash">
root@user:~# find -name *.sh
root@user:~# find -name *.sh
</source>
<source lang="bash">
root@tecmint:~# find -name *.tar.gz
</source>
</source>



Revision as of 17:51, 7 May 2017

Using shell scripting to automate Linux maintenance tasks

Author

Translated from Estonian by Steven Rugam

Short introduction

Bash (Bourne-again shell) is a well-known shell. Bash scripts are used to simplify and automate system work. When talking about bash in few words, then the structure of the language and possible usages are being checked. At length, however, focus lies on the certain administrating assignments which are solved with bash scripting language. It is a powerful shell, and features, among other things:

    • Command line editing
    • Command history
    • A directory stack (pushd, popd)
    • Command substitution
    • Special variables, like $PPID
    • Autocompletion
    • In-process integer arithmetic: $((...))
    • In-process regexes
    • Aliases
    • Functions
    • Arrays
    • Expansions: tilde, brace, variable
    • Substring awesomeness
    • Conditional expressions
    • Security (restricted shell mode)
    • Job Control
    • Timing
    • Prompt customization

Purpose

Providing skills for the bash scripting language to simplify and to improve efficiency towards our daily work.

Prerequisites/Assumptions

History with GNU/Linux operation system and acquired skills, which are being tested by automated self-test.

To test prerequisites, you should pass self-test.

Target group

Support engineers and system administrators.

Educational materials for learning BASH shell

Bash for the beginners

Additional learning materials

https://bash.cyberciti.biz/guide/Main_Page

Using Bash for administrating

Command: find

Search for files in the given directory, hierarchically starting at the parent directory and moving to sub-directories.

root@user:~# find -name *.sh

Login Script

When bash is invoked, it runs /etc/profile if that file exists. Next, it looks for these files (in this order) ~/.bash_profile, ~/.bash_login , and ~/.profile. The first one that is found gets executed (any others are ignored). In the case of Ubuntu, ~/.profile is found and executed. (All of ~/.bash_profile, ~/.bash_login , and ~/.profile are ignored if --noprofile is used as an option to the underlying bash call). So for example, if you login via a virtual console, or if you change to another user e.g:

sudo su student2

or if u run:

bash --login

or if you login to a machine via ssh e.g:

ssh user@machine

You'll be invoking ~/.profile Note that ~/.profile (or if not there, one of ~/.bash_profile or ~/.bash_login) normally contains relevant commands to run another script called ~/.bashrc (if said file exists):

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

Topics

  • Bash in general
    • What is bash and shell
    • Bash script writing
    • Executing bash scripts
  • Bash configuration files
    • .bash_profile: .bash_login ja .profile
    • .bashrc
    • .bash_logout
    • /etc/profile, /etc/profile.d/
    • Bash command history in .bash_history
  • Bash Syntax
    • Commenting
    • Symbols with special meaning(metacharacters, escaping characters, wildcards)
    • Difference between quotation marks and single quotation mark
    • Parameters
    • Catching user input and interactive scripts
    • Exit codes
    • Multiple commands on the same row
    • Piping
    • If sentences (if-else, case)
    • Cycles (for, while, until, select)
    • Control of strings (larger than, larger/smaller than or equal, smaller than)
    • File descriptors (stdin, stdout, stderr)
    • File existance scripts
    • Functions
    • Arithmetic operations, rounding
    • Regular expressions
    • Usage of arrays
  • Basics of bash
    • Utilities and their usage (echo, cat, cut, wc, sed, awk, mv, cp, mkdir, ls, file, head, uniq, tail, etc.)
    • Processes and executing them (ps, pstree, top, &)
    • Detecting shell script mistakes and fixing them

Example exercises

  • Calculation exercises
  • String manipulations
  • Exercises with files
  • Writing scripts, which show system configurations (logged in users, OS version, RAM info, Partitions etc)
  • Stopping processes and restarting processes
  • Adding new domains to DNS servers from the shell
  • Converting file lowercase characters to uppercase characters

Links

http://gnome-look.org/content/show.php/Ultimate+Bashrc+File?content=129746 hacking .bashrc

References