SlideShare a Scribd company logo
bashin theory and in practice
19.06.2013
03.07.2013 @pvb265 #imolug
abstract
19.06.2013
03.07.2013 @pvb265 #imolug
abstract
19.06.2013
03.07.2013 @pvb265 #imolug
shell
bash
kernel
o.s.
history
abstract
19.06.2013
03.07.2013 @pvb265 #imolug
shell
bash
kernel
o.s.
Login Process
history
passwd
shadow
shells
configurationfiles
abstract
19.06.2013
03.07.2013 @pvb265 #imolug
shell
bash
kernel
o.s.
Login Process
history
passwd
shadow
shells
configurationfiles
Main Features
POSIX
completion
cmdhistory
AgendaGeneral Info
Underwater
Overview
Syntax
Example
19.06.2013
03.07.2013 @pvb265 #imolug
19.06
03.07
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Syntax
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Syntaxσύνταξις "arrangement" from σύν syn, "together", and τάξις táxis, "an ordering"
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
comment #à #
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
comment #
singole quote ' X ' ' ' '?
'
à #
any trasformations
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
comment #
singole quote ' X ' ' ' '?
'
à #
any trasformations
double quote “ X ”“
2
any trasformations
buts $ ~  ! * @
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
comment #
singole quote ' X ' ' ' '?
'
à #
any trasformations
double quote “ X ”“
2
any trasformations
buts $ ~  ! * @
ANSI-C quoting |
 b backspace
f form feed
n newline
r carriage return
t horizontal tab
v vertical tab
 backslash
' single quote
" double quote
nnn the octal value nnn
xHH the hexadecimal value HH
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
variable
vector
NAME=”test” echo $NAME
NAME=(test1 test2 test3) echo $TEST[0]
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
MacBook-Pro-di-valerio:~ valeriobalbi$ echo "the shell I'm using is $SHELL and his value is in " '$SHELL'
the shell I'm using is /bin/bash and his value is in $SHELL
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until test-command; do commands; done
while test-command; do commands; done
for name in lists; do commands; done
for (( exp1; exp2; exp3 )); do commands; done
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until test-command; do commands; done
while test-command; do commands; done
for name in lists; do commands; done
for (( exp1; exp2; exp3 )); do commands; done
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until test-command; do commands; done
while test-command; do commands; done
for name in lists; do commands; done
for (( exp1; exp2; exp3 )); do commands; done
MacBook-Pro-di-valerio:~ valeriobalbi$ export i=2
MacBook-Pro-di-valerio:~ valeriobalbi$ until [ $i -lt 0 ]; do echo $i; export i=$(($i-1)); done
2
1
0
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until test-command; do commands; done
while test-command; do commands; done
for name in lists; do commands; done
for (( exp1; exp2; exp3 )); do commands; done
MacBook-Pro-di-valerio:~ valeriobalbi$ export i=2
MacBook-Pro-di-valerio:~ valeriobalbi$ while [ $i -ge 0 ]; do echo $i; export i=$(($i-1)); done
2
1
0
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until test-command; do commands; done
while test-command; do commands; done
for name in lists; do commands; done
for (( exp1; exp2; exp3 )); do commands; done
MacBook-Pro-di-valerio:~ valeriobalbi$ while true; do date; sleep 2; done
Mar 2 Lug 2013 00:58:07 CEST
Mar 2 Lug 2013 00:58:09 CEST
Mar 2 Lug 2013 00:58:11 CEST
^C
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until test-command; do commands; done
while test-command; do commands; done
for name in lists; do commands; done
for (( exp1; exp2; exp3 )); do commands; done
MacBook-Pro-di-valerio:~ valeriobalbi$ for i in 2 1 0; do echo $i; done
2
1
0
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until test-command; do commands; done
while test-command; do commands; done
for name in lists; do commands; done
for (( exp1; exp2; exp3 )); do commands; done
MacBook-Pro-di-valerio:~ valeriobalbi$ for i in $(ls); do echo $i; done
.
..
file_one
file_two
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until test-command; do commands; done
while test-command; do commands; done
for name in lists; do commands; done
for (( exp1; exp2; exp3 )); do commands; done
MacBook-Pro-di-valerio:~ valeriobalbi$ for (( i=0 ; $i<3; i++ )); do echo $i; done
0
1
2
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until while for command
if test-command; then commands; ficonditional
if test-command; then commands; else commands; fi
if test-command; then commands; elif commands; else commands; fi
if test-command; then commands; (elif commands;)* else commands; fi
case name in value) commands;; … value) commands;; esac;
select name in words; do commands; done
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until while for command
if test-command; then commands; ficonditional
if test-command; then commands; else commands; fi
if test-command; then commands; elif commands; else commands; fi
if test-command; then commands; (elif commands;)* else commands; fi
case name in (value) commands;; )+ esac commands;
select name in words; do commands; done
MacBook-Pro-di-valerio:~ valeriobalbi$ export i=start; case $i in
> start)
> echo "start command placeholder";;
> stop)
> echo "stopping command placeholder";;
> esac
Start command placeholder
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until while for command
if test-command; then commands; ficonditional
if test-command; then commands; else commands; fi
if test-command; then commands; elif commands; else commands; fi
if test-command; then commands; (elif commands;)* else commands; fi
case name in (value) commands;; )+ esac commands;
select name in words; do commands; done
MacBook-Pro-di-valerio:~ valeriobalbi$ export i=”one two”; select j in $i; do echo $j; done
1) one
2) two
#? 1
one
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until while for command
if case select commandconditional
arithmetics
logical complex
(( expression ))
[[ expression ]]
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until while for command
if case select commandconditional
arithmetics
logical complex
MacBook-Pro-di-valerio:~ valeriobalbi$ export i=10; ((i--)); echo $i
9
(( expression ))
[[ expression ]]
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
command verb parameter1 parameter2 parameter3 ...
looping until while for command
if case select commandconditional
arithmetics
logical complex
MacBook-Pro-di-valerio:~ valeriobalbi$ if [[ -a /etc/passwd || -b /etc/passwd ]]; then ls -l /etc/passwd; fi
-rw-r--r-- 1 root wheel 5148 18 Nov 2011 /etc/passwd
MacBook-Pro-di-valerio:~ valeriobalbi$ if [[ -a /etc/passwd && -b /etc/passwd ]]; then ls -l /etc/passwd; fi
MacBook-Pro-di-valerio:~ valeriobalbi$
(( expression ))
[[ expression ]]
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
parentheses ( list )
curly brace
new environment, space needed
{ list; } same environment, operational token
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
parentheses ( list )
curly brace
new environment, space needed
{ list; } same environment, operational token
function name ( ) { commands; } redirections
name ( ) { commands; } redirections
undef name
FUNCNEST != 0
local definition variable
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
parentheses ( list )
curly brace
MacBook-Pro-di-valerio:~ valeriobalbi$ foo(){
# do something
# if not false call foo
foo
}
MacBook-Pro-di-valerio:~ valeriobalbi$ foo
new environment, space needed
{ list; } same environment, operational token
function name ( ) { commands; } redirections
name ( ) { commands; } redirections
undef name
FUNCNEST != 0
local definition variable
infinite loop
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
parentheses ( list )
curly brace
MacBook-Pro-di-valerio:~ valeriobalbi$ export FUNCNEST=5
MacBook-Pro-di-valerio:~ valeriobalbi$ foo(){
# do something
# if not false call foo
foo
}
MacBook-Pro-di-valerio:~ valeriobalbi$ foo
MacBook-Pro-di-valerio:~ valeriobalbi$ undef foo
MacBook-Pro-di-valerio:~ valeriobalbi$ foo
-bash: foo: command not found
new environment, space needed
{ list; } same environment, operational token
function name ( ) { commands; } redirections
name ( ) { commands; } redirections
undef name
FUNCNEST != 0
local definition variable
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
parentheses ( list )
curly brace
new environment, space needed
{ list; } same environment, operational token
built-in function
function definition
. filename
break n
cd ..
cd -
cd directory
eval
exec command
exit n
export
pwd
readonly name
return n
shift n
test
umask mode
unset name
bash specificsh specific
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
parentheses ( list )
curly brace
new environment, space needed
{ list; } same environment, operational token
built-in function
function definition
. filename
break n
cd ..
cd -
cd directory
eval
exec command
exit n
export
pwd
readonly name
return n
shift n
test
umask mode
unset name
alias name=value
command name
echo
help
local name=value
let
printf format values
read n
source filename
ulimit
unalias
bash specificsh specific
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
positional
special
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
positional
special
useradd -b /home/pvb265 pvb265
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
positional useradd -b /home/pvb265 pvb265
${0} ${1} ${2} ${3}
special
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
positional useradd -b /home/pvb265 pvb265
special $* equivalent ${1}${2}${3}....
$@ equivalent ${1} ${2} ${3}
$# parameter number
$? return code
$$ PID
${0} ${1} ${2} ${3}
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
$CDPATH
$HOME
$IFS
$PATH
$PS1
$PS2
bash specificsh specific
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
$CDPATH
$HOME
$IFS
$PATH
$PS1
$PS2
$BASH*
$ENV
$HISTSIZE
$HISTFILESIZE
LANG
SHELL
PPID
UID
GID
bash specificsh specific
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
process1
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
process1
stdin
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
process1
stdin stdout
stderr
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
process1
stdin stdout
stderr
0 1
2
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
cat /etc/hosts > /tmp/output.txt
process1
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
cat /etc/hosts > /tmp/output.txt
cat /etc/passwd >> /tmp/output.txt
process1
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
cat /etc/hosts > /tmp/output.txt
cat /etc/passwd >> /tmp/output.txt
cat /etc/passwd 2>&1 > /tmp/output.txt
process1
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
cat /etc/hosts > /tmp/output.txt
cat /etc/passwd >> /tmp/output.txt
cat /etc/passwd 2>&1 > /tmp/output.txt
> /tmp/output.txt
process1
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
process1 process2
pipeline
||
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
Script is a collection of commands
#!/bin/sh
#
RETVAL=0
prog="sssd"
# Source function library.
. /etc/init.d/functions
SSSD=/usr/sbin/sssd
LOCK_FILE=/var/lock/subsys/sssd
PID_FILE=/var/run/sssd.pid
start() {
[ -x $SSSD ] || exit 5
echo -n $"Starting $prog: "
daemon $SSSD -f -D && success || failure
RETVAL=$?
echo
[ "$RETVAL" = 0 ] && touch $LOCK_FILE
return $RETVAL
}
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
shabang #! #!/bin/bash
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
shabang #! #!/bin/bash
code syntax tree
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
shabang #! #!/bin/bash
code syntax tree
executable code
execution
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
shabang #! #!/bin/bash
code syntax tree execution
shabang tell O.S. which interpreter use to execute the code
For each #! line immagine the fork of a new interpreter
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Basics
Commands
Functions
Parameters
Variables
Redirections
Scripting
Syntax
shabang #! #!/bin/bash
code syntax tree execution
a script file can be executed
- bash scriptfilename.sh
- scriptfilename.sh
19.06.2013
03.07.2013 @pvb265 #imolug
bash
Example
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
[root@doc ~]# runlevel
N 3
[root@doc ~]# cd /etc/rc+TAB+TAB
rc rc0.d/ rc1.d/ rc2.d/ rc3.d/ rc4.d/ rc5.d/ rc6.d/ rc.d/ rc.local rc.sysinit
[root@doc ~]# cd /etc/rc3.d/
[root@doc rc3.d]# ls -l
totale 0
lrwxrwxrwx. 1 root root 15 22 giu 23:52 K01numad -> ../init.d/numad
lrwxrwxrwx. 1 root root 16 22 giu 23:57 K01smartd -> ../init.d/smartd
lrwxrwxrwx. 1 root root 17 22 giu 21:21 K02oddjobd -> ../init.d/oddjobd
lrwxrwxrwx. 1 root root 16 22 giu 23:58 K10psacct -> ../init.d/psacct
lrwxrwxrwx. 1 root root 19 22 giu 23:57 K10saslauthd -> ../init.d/saslauthd
…
lrwxrwxrwx. 1 root root 14 22 giu 23:57 S55sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root 16 23 giu 19:42 S64mysqld -> ../init.d/mysqld
lrwxrwxrwx. 1 root root 17 22 giu 21:21 S80postfix -> ../init.d/postfix
...
lrwxrwxrwx. 1 root root 11 22 giu 23:48 S99local -> ../rc.local
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
[root@doc rc3.d]# cd ../init.d/
[root@doc init.d]# ls -l
totale 320
-rwxr-xr-x. 1 root root 1288 23 feb 05:39 abrt-ccpp
…
-rwxr-xr-x. 1 root root 1698 22 feb 06:38 sandbox
-rwxr-xr-x. 1 root root 2056 20 nov 2012 saslauthd
-rwxr-xr-x. 1 root root 647 9 gen 12:13 single
-rwxr-xr-x. 1 root root 3002 21 feb 23:26 smartd
-rwxr-xr-x. 1 root root 4534 22 feb 04:51 sshd
-rwxr-xr-x. 1 root root 2647 29 apr 09:50 sssd
-rwxr-xr-x. 1 root root 1228 22 giu 2012 sysstat
-rwxr-xr-x. 1 root root 2294 22 feb 06:22 udev-post
-rwxr-xr-x. 1 root root 1608 22 feb 05:09 winbind
-rwxr-xr-x. 1 root root 4799 22 feb 00:23 ypbind
[root@doc init.d]# file sshd
sshd: Bourne-Again shell script text executable
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
#!/bin/bash
#
# sshd Start up the OpenSSH server daemon
#
# chkconfig: 2345 55 25
# description: SSH is a protocol for secure remote shell access. 
# This service starts up the OpenSSH server daemon.
#
# processname: sshd
# config: /etc/ssh/ssh_host_key
# config: /etc/ssh/ssh_host_key.pub
# config: /etc/ssh/ssh_random_seed
# config: /etc/ssh/sshd_config
# pidfile: /var/run/sshd.pid
### BEGIN INIT INFO
# Provides: sshd
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $syslog
# Should-Start: $syslog
# Should-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
#!/bin/bash
#
# sshd Start up the OpenSSH server daemon
#
# chkconfig: 2345 55 25
# description: SSH is a protocol for secure remote shell access. 
# This service starts up the OpenSSH server daemon.
#
# processname: sshd
# config: /etc/ssh/ssh_host_key
# config: /etc/ssh/ssh_host_key.pub
# config: /etc/ssh/ssh_random_seed
# config: /etc/ssh/sshd_config
# pidfile: /var/run/sshd.pid
### BEGIN INIT INFO
# Provides: sshd
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $syslog
# Should-Start: $syslog
# Should-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
# Short-Description: Start up the OpenSSH server daemon
# Description: SSH is a protocol for secure remote shell access.
# This service starts up the OpenSSH server daemon.
### END INIT INFO
# source function library
. /etc/rc.d/init.d/functions
# pull in sysconfig settings
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
RETVAL=0
prog="sshd"
lockfile=/var/lock/subsys/$prog
# Some functions to make the below more readable
KEYGEN=/usr/bin/ssh-keygen
SSHD=/usr/sbin/sshd
RSA1_KEY=/etc/ssh/ssh_host_key
RSA_KEY=/etc/ssh/ssh_host_rsa_key
DSA_KEY=/etc/ssh/ssh_host_dsa_key
PID_FILE=/var/run/sshd.pid
runlevel=$(set -- $(runlevel); eval "echo $$#" )
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
# Short-Description: Start up the OpenSSH server daemon
# Description: SSH is a protocol for secure remote shell access.
# This service starts up the OpenSSH server daemon.
### END INIT INFO
# source function library
. /etc/rc.d/init.d/functions
# pull in sysconfig settings
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
RETVAL=0
prog="sshd"
lockfile=/var/lock/subsys/$prog
# Some functions to make the below more readable
KEYGEN=/usr/bin/ssh-keygen
SSHD=/usr/sbin/sshd
RSA1_KEY=/etc/ssh/ssh_host_key
RSA_KEY=/etc/ssh/ssh_host_rsa_key
DSA_KEY=/etc/ssh/ssh_host_dsa_key
PID_FILE=/var/run/sshd.pid
runlevel=$(set -- $(runlevel); eval "echo $$#" )
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
# Short-Description: Start up the OpenSSH server daemon
# Description: SSH is a protocol for secure remote shell access.
# This service starts up the OpenSSH server daemon.
### END INIT INFO
# source function library
. /etc/rc.d/init.d/functions
# pull in sysconfig settings
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
RETVAL=0
prog="sshd"
lockfile=/var/lock/subsys/$prog
# Some functions to make the below more readable
KEYGEN=/usr/bin/ssh-keygen
SSHD=/usr/sbin/sshd
RSA1_KEY=/etc/ssh/ssh_host_key
RSA_KEY=/etc/ssh/ssh_host_rsa_key
DSA_KEY=/etc/ssh/ssh_host_dsa_key
PID_FILE=/var/run/sshd.pid
runlevel=$(set -- $(runlevel); eval "echo $$#" )
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
# Short-Description: Start up the OpenSSH server daemon
# Description: SSH is a protocol for secure remote shell access.
# This service starts up the OpenSSH server daemon.
### END INIT INFO
# source function library
. /etc/rc.d/init.d/functions
# pull in sysconfig settings
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
RETVAL=0
prog="sshd"
lockfile=/var/lock/subsys/$prog
# Some functions to make the below more readable
KEYGEN=/usr/bin/ssh-keygen
SSHD=/usr/sbin/sshd
RSA1_KEY=/etc/ssh/ssh_host_key
RSA_KEY=/etc/ssh/ssh_host_rsa_key
DSA_KEY=/etc/ssh/ssh_host_dsa_key
PID_FILE=/var/run/sshd.pid
runlevel=$(set -- $(runlevel); eval "echo $$#" )
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
# Short-Description: Start up the OpenSSH server daemon
# Description: SSH is a protocol for secure remote shell access.
# This service starts up the OpenSSH server daemon.
### END INIT INFO
# source function library
. /etc/rc.d/init.d/functions
# pull in sysconfig settings
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
RETVAL=0
prog="sshd"
lockfile=/var/lock/subsys/$prog
# Some functions to make the below more readable
KEYGEN=/usr/bin/ssh-keygen
SSHD=/usr/sbin/sshd
RSA1_KEY=/etc/ssh/ssh_host_key
RSA_KEY=/etc/ssh/ssh_host_rsa_key
DSA_KEY=/etc/ssh/ssh_host_dsa_key
PID_FILE=/var/run/sshd.pid
runlevel=$(set -- $(runlevel); eval "echo $$#" )
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
# Short-Description: Start up the OpenSSH server daemon
# Description: SSH is a protocol for secure remote shell access.
# This service starts up the OpenSSH server daemon.
### END INIT INFO
# source function library
. /etc/rc.d/init.d/functions
# pull in sysconfig settings
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
RETVAL=0
prog="sshd"
lockfile=/var/lock/subsys/$prog
# Some functions to make the below more readable
KEYGEN=/usr/bin/ssh-keygen
SSHD=/usr/sbin/sshd
RSA1_KEY=/etc/ssh/ssh_host_key
RSA_KEY=/etc/ssh/ssh_host_rsa_key
DSA_KEY=/etc/ssh/ssh_host_dsa_key
PID_FILE=/var/run/sshd.pid
runlevel=$(set -- $(runlevel); eval "echo $$#" )
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
# Short-Description: Start up the OpenSSH server daemon
# Description: SSH is a protocol for secure remote shell access.
# This service starts up the OpenSSH server daemon.
### END INIT INFO
# source function library
. /etc/rc.d/init.d/functions
# pull in sysconfig settings
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
RETVAL=0
prog="sshd"
lockfile=/var/lock/subsys/$prog
# Some functions to make the below more readable
KEYGEN=/usr/bin/ssh-keygen
SSHD=/usr/sbin/sshd
RSA1_KEY=/etc/ssh/ssh_host_key
RSA_KEY=/etc/ssh/ssh_host_rsa_key
DSA_KEY=/etc/ssh/ssh_host_dsa_key
PID_FILE=/var/run/sshd.pid
runlevel=$(set -- $(runlevel); eval "echo $$#" )
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
fips_enabled() {
if [ -r /proc/sys/crypto/fips_enabled ]; then
cat /proc/sys/crypto/fips_enabled
else
echo 0
fi
}
do_rsa1_keygen() {
if [ ! -s $RSA1_KEY -a `fips_enabled` -eq 0 ]; then
echo -n $"Generating SSH1 RSA host key: "
rm -f $RSA1_KEY
if test ! -f $RSA1_KEY && $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then
chmod 600 $RSA1_KEY
chmod 644 $RSA1_KEY.pub
if [ -x /sbin/restorecon ]; then
/sbin/restorecon $RSA1_KEY.pub
fi
success $"RSA1 key generation"
echo
else
failure $"RSA1 key generation"
echo
exit 1
fi
fi
}
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
fips_enabled() {
if [ -r /proc/sys/crypto/fips_enabled ]; then
cat /proc/sys/crypto/fips_enabled
else
echo 0
fi
}
do_rsa1_keygen() {
if [ ! -s $RSA1_KEY -a `fips_enabled` -eq 0 ]; then
echo -n $"Generating SSH1 RSA host key: "
rm -f $RSA1_KEY
if test ! -f $RSA1_KEY && $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then
chmod 600 $RSA1_KEY
chmod 644 $RSA1_KEY.pub
if [ -x /sbin/restorecon ]; then
/sbin/restorecon $RSA1_KEY.pub
fi
success $"RSA1 key generation"
echo
else
failure $"RSA1 key generation"
echo
exit 1
fi
fi
}
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
fips_enabled() {
if [ -r /proc/sys/crypto/fips_enabled ]; then
cat /proc/sys/crypto/fips_enabled
else
echo 0
fi
}
do_rsa1_keygen() {
if [ ! -s $RSA1_KEY -a `fips_enabled` -eq 0 ]; then
echo -n $"Generating SSH1 RSA host key: "
rm -f $RSA1_KEY
if test ! -f $RSA1_KEY && $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then
chmod 600 $RSA1_KEY
chmod 644 $RSA1_KEY.pub
if [ -x /sbin/restorecon ]; then
/sbin/restorecon $RSA1_KEY.pub
fi
success $"RSA1 key generation"
echo
else
failure $"RSA1 key generation"
echo
exit 1
fi
fi
}
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
start()
{
[ -x $SSHD ] || exit 5
[ -f /etc/ssh/sshd_config ] || exit 6
# Create keys if necessary
if [ "x${AUTOCREATE_SERVER_KEYS}" != xNO ]; then
do_rsa1_keygen
do_rsa_keygen
do_dsa_keygen
fi
echo -n $"Starting $prog: "
$SSHD $OPTIONS && success || failure
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $lockfile
echo
return $RETVAL
}
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
start()
{
[ -x $SSHD ] || exit 5
[ -f /etc/ssh/sshd_config ] || exit 6
# Create keys if necessary
if [ "x${AUTOCREATE_SERVER_KEYS}" != xNO ]; then
do_rsa1_keygen
do_rsa_keygen
do_dsa_keygen
fi
echo -n $"Starting $prog: "
$SSHD $OPTIONS && success || failure
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $lockfile
echo
return $RETVAL
}
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
start()
{
[ -x $SSHD ] || exit 5
[ -f /etc/ssh/sshd_config ] || exit 6
# Create keys if necessary
if [ "x${AUTOCREATE_SERVER_KEYS}" != xNO ]; then
do_rsa1_keygen
do_rsa_keygen
do_dsa_keygen
fi
echo -n $"Starting $prog: "
$SSHD $OPTIONS && success || failure
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $lockfile
echo
return $RETVAL
}
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
case "$1" in
start)
rh_status_q && exit 0
start
;;
stop)
if ! rh_status_q; then
rm -f $lockfile
exit 0
fi
stop
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}"
RETVAL=2
esac
exit $RETVAL
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
case "$1" in
start)
rh_status_q && exit 0
start
;;
stop)
if ! rh_status_q; then
rm -f $lockfile
exit 0
fi
stop
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}"
RETVAL=2
esac
exit $RETVAL
19.06.2013
03.07.2013 @pvb265 #imolug
bash Example
Questions?

More Related Content

PDF
Bash in theory and in practice - part one
PDF
PDF
PDF
C: A Humbling Language
PPT
Happy porting x86 application to android
PPTX
Unix shell scripting basics
ODP
Eat my data
PDF
Shell scripting
Bash in theory and in practice - part one
C: A Humbling Language
Happy porting x86 application to android
Unix shell scripting basics
Eat my data
Shell scripting

What's hot (19)

PDF
Be pinched by a cRUSTacean to prevent programming errors !
PDF
Bash is not a second zone citizen programming language
PPT
Unix Shell Scripting Basics
PDF
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
PPT
Unix Shell Scripting Basics
PDF
Containers for sysadmins
PPT
Talk Unix Shell Script
PPT
Unix And C
PDF
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
PPTX
Unix shell scripting
PPT
Talk Unix Shell Script 1
ODP
DevChatt 2010 - *nix Cmd Line Kung Foo
PDF
Introduction to shell scripting
PPT
Airlover 20030324 1
PPT
PHP-FIG: Past, Present and Future
PPT
Unix Basics
PPT
On UnQLite
KEY
Introducing Command Line Applications with Ruby
PPT
01 linux basics
Be pinched by a cRUSTacean to prevent programming errors !
Bash is not a second zone citizen programming language
Unix Shell Scripting Basics
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Unix Shell Scripting Basics
Containers for sysadmins
Talk Unix Shell Script
Unix And C
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Unix shell scripting
Talk Unix Shell Script 1
DevChatt 2010 - *nix Cmd Line Kung Foo
Introduction to shell scripting
Airlover 20030324 1
PHP-FIG: Past, Present and Future
Unix Basics
On UnQLite
Introducing Command Line Applications with Ruby
01 linux basics
Ad

Similar to Bash in theory and in practice - part two (20)

PDF
Becoming a Git Master - Nicola Paolucci
PDF
BSDM with BASH: Command Interpolation
PPT
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
PPT
ShellAdvanced shell scripting programm.ppt
PDF
Will iPython replace Bash?
PDF
Will iPython replace bash?
PDF
Overloading Perl OPs using XS
PDF
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
PDF
Gogo shell
PDF
Performance Profiling in Rust
PDF
Hudson以外の何か with 任意
DOCX
PrizeExample.DS_Store__MACOSXPrizeExample._.DS_StoreP.docx
PDF
Functional Structures in PHP
PDF
Es6 to es5
PPTX
KT on Bash Script.pptx
PPT
Best training-in-mumbai-shell scripting
PDF
Idiomatic Javascript (ES5 to ES2015+)
PPTX
PHP 5.3
PDF
Nginx Workshop Aftermath
PPT
Perl Intro 9 Command Line Arguments
Becoming a Git Master - Nicola Paolucci
BSDM with BASH: Command Interpolation
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced shell scripting programm.ppt
Will iPython replace Bash?
Will iPython replace bash?
Overloading Perl OPs using XS
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
Gogo shell
Performance Profiling in Rust
Hudson以外の何か with 任意
PrizeExample.DS_Store__MACOSXPrizeExample._.DS_StoreP.docx
Functional Structures in PHP
Es6 to es5
KT on Bash Script.pptx
Best training-in-mumbai-shell scripting
Idiomatic Javascript (ES5 to ES2015+)
PHP 5.3
Nginx Workshop Aftermath
Perl Intro 9 Command Line Arguments
Ad

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Spectroscopy.pptx food analysis technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
KodekX | Application Modernization Development
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Machine learning based COVID-19 study performance prediction
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Approach and Philosophy of On baking technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
Review of recent advances in non-invasive hemoglobin estimation
Spectroscopy.pptx food analysis technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
KodekX | Application Modernization Development
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Programs and apps: productivity, graphics, security and other tools
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction
NewMind AI Weekly Chronicles - August'25 Week I
Approach and Philosophy of On baking technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
MIND Revenue Release Quarter 2 2025 Press Release
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Per capita expenditure prediction using model stacking based on satellite ima...
Advanced methodologies resolving dimensionality complications for autism neur...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Understanding_Digital_Forensics_Presentation.pptx

Bash in theory and in practice - part two