Bash cheatsheet: Difference between revisions
From Lolly's Wiki
Jump to navigationJump to search
No edit summary |
|||
Line 45: | Line 45: | ||
=Nützliche Variablenersetzungen= | =Nützliche Variablenersetzungen= | ||
==dirname== | ==dirname== | ||
< | <source lang=bash> | ||
$ myself=/usr/bin/blafasel ; echo ${myself%/*} | $ myself=/usr/bin/blafasel ; echo ${myself%/*} | ||
/usr/bin | /usr/bin | ||
</ | </source> | ||
==basename== | ==basename== | ||
< | <source lang=bash> | ||
$ myself=/usr/bin/blafasel ; echo ${myself##*/} | $ myself=/usr/bin/blafasel ; echo ${myself##*/} | ||
blafasel | blafasel | ||
</ | </source> | ||
==Path name resolving function== | |||
<source lang=bash> | |||
# dir_resolve originally from http://stackoverflow.com/a/20901614/5887626 | |||
# modified at https://lars.timmann.de/wiki | |||
dir_resolve() { | |||
local dir=${1%/*} | |||
local file=${1##*/} | |||
# if the name does not contain a / leave file blank or the name will be name/name | |||
[ "_${1/\//}_" == "_${1}_" -a -d ${1} ] && file="" | |||
[ "_${1/\//}_" == "_${1}_" -a -f ${1} ] && dir="" | |||
pushd "$dir" &>/dev/null || return $? # On error, return error code | |||
echo ${PWD}${file:+"/"${file}} # output full path with filename | |||
popd &> /dev/null | |||
} | |||
</source> | |||
=Schleifen= | =Schleifen= | ||
==Zahlenfolgen== | ==Zahlenfolgen== |
Revision as of 12:42, 19 February 2016
bash history per user
You need to set LogLevel of sshd to VERBOSE in your /etc/ssh/sshd_config:
...
LogLevel VERBOSE
...
If you are using ssh public keys for authenticating and want to use a seperate history for each user, you can put this in your .bash_profile:
[ -f /var/log/fingerprint.log ] && FINGERPRINT=$(nawk -v ssh_connection="${SSH_CONNECTION}" -v user=${LOGNAME} 'BEGIN{split(ssh_connection,connection)}/.*sshd\[[0-9]+\]: Accepted publickey for/ && $(NF-5)==connection[1] && $(NF-3)==connection[2] {print $NF;}' /var/log/fingerprint.log)
export HISTFILE=~/.bash_history_${FINGERPRINT:-${SUDO_USER:-default}}
If $FINGERPRINT is empty the sudo user will be used. If $SUDO_USER is empty too, use "default" as extension.
I forced rsyslog to write another logfile where group ssh may read: /etc/rsyslog.d/99-fingerprint.conf:
$FileCreateMode 0640
$FileGroup ssh
auth /var/log/fingerprint.log
Add user syslog to group ssh so that syslog can open a file as group ssh:
# usermod -aG ssh syslog
Let only users from group ssh login via ssh except the syslog user: /etc/ssh/sshd_config:
# SSH is only allowed for users in this group
AllowGroups ssh
DenyUsers syslog
bash prompt
Put this in your ~/.bash_profile
typeset +x PS1="\[\e]0;\u@\h: \w\a\]\u@\h:\w# "
Nützliche Variablenersetzungen
dirname
$ myself=/usr/bin/blafasel ; echo ${myself%/*}
/usr/bin
basename
$ myself=/usr/bin/blafasel ; echo ${myself##*/}
blafasel
Path name resolving function
# dir_resolve originally from http://stackoverflow.com/a/20901614/5887626
# modified at https://lars.timmann.de/wiki
dir_resolve() {
local dir=${1%/*}
local file=${1##*/}
# if the name does not contain a / leave file blank or the name will be name/name
[ "_${1/\//}_" == "_${1}_" -a -d ${1} ] && file=""
[ "_${1/\//}_" == "_${1}_" -a -f ${1} ] && dir=""
pushd "$dir" &>/dev/null || return $? # On error, return error code
echo ${PWD}${file:+"/"${file}} # output full path with filename
popd &> /dev/null
}
Schleifen
Zahlenfolgen
$ for i in {0..9} ; do echo $i ; done
oder
$ for ((i=0;i<=9;i++)); do echo $i; done
so gehen natürlich auch andere Sprünge, z.B. immer 3 weiter:
$ for ((i=0;i<=9;i+=3)); do echo $i; done
oder oder oder
$ for ((i=0,j=1;i<=9;i+=3,j++)); do echo "$i $j"; done
Rechnen
$ echo $[ 3 + 4 ] $ echo $[ 2 ** 8 ] # 2^8