BASH shell en: Difference between revisions

From ICO wiki
Jump to navigationJump to search
Line 103: Line 103:


===Command: whereis===
===Command: whereis===
The ‘whereis‘ command is used to locate the Binary, Sources and Manual Pages of the command. For example, to locate the Binary, Sources and Manual Pages of the command ‘ls‘ and ‘kill‘.
<source lang="bash">
root@user:~# whereis ls
</source>
<source lang="bash">
root@user:~# whereis kill
</source>
Note: This is useful to know where the binaries are installed for manual editing sometimes.
===Command: service===
===Command: service===
===Command: alias===
===Command: alias===

Revision as of 18:07, 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

Command: grep

root@user:~# grep user /etc/passwd 
user:x:1000:1000:user,,,:/home/user:/bin/bash

Ignore word case and all other combination with ‘-i‘ option.

root@user:~# grep -i USER /etc/passwd 
user:x:1000:1000:User,,,:/home/user:/bin/bash

Search recursively (-r) i.e. read all files under each directory for a string “127.0.0.1“.

root@user:~# grep -r "127.0.0.1" /etc/

Command: man

The ‘man‘ is the system’s manual pager. Man provides online documentation for all the possible options with a command and its usages. Almost all the command comes with their corresponding manual pages. For example,

root@user:~# man bash

Manual page for man page itself, similarly ‘man cat‘ (Manual page for cat command) and ‘man ls‘ (Manual page for command ls).

Command: ps

ps (Process) gives the status of running processes with a unique Id called PID.

root@user:~# ps

To list status of all the processes along with process id and PID, use option ‘-A‘.

root@user:~# ps -A

Note: This command is very useful when you want to know which processes are running or may need PID sometimes, for process to be killed. You can use it with ‘grep‘ command to find customised output. For example,

root@user:~# ps -A | grep -i ssh

Here ‘ps‘ is pipelined with ‘grep‘ command to find customised and relevant output of our need.

Command: kill

You need a process’s pid (ps) to kill it. Let suppose you want to kill program ‘apache2‘ that might not be responding. Run ‘ps -A‘ along with grep command.

root@user:~# ps -A | grep -i apache2
1285 ?        00:00:00 apache2

Find process ‘apache2‘, note its pid and kill it. For example, in that case ‘apache2‘ pid is ‘1285‘.

root@user:~# kill 1285 (to kill the process apache2)

Note: Every time you re-run a process or start a system, a new pid is generated for each process and you can know about the current running processes and its pid using command ‘ps‘. Another way to kill the same process is.

root@user:~# pkill apache2

Note: Kill requires job id / process id for sending signals, where as in pkill, you have an option of using pattern, specifying process owner, etc.

Command: whereis

The ‘whereis‘ command is used to locate the Binary, Sources and Manual Pages of the command. For example, to locate the Binary, Sources and Manual Pages of the command ‘ls‘ and ‘kill‘.

root@user:~# whereis ls
root@user:~# whereis kill

Note: This is useful to know where the binaries are installed for manual editing sometimes.

Command: service

Command: alias

Command: df

Command: du

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