Close
Edit

category: prog, direction: bash, group: django

bash

bash

postfix

источник: Установка и настройка Postfix в Ubuntu 20.04

ключи: #postfix #email

Содержание >>

только важные выдержки


install

From the man bash pages (especially the CONDITIONAL EXPRESSIONS section):

pip install django-environ
if [ -x /etc/rc.local ] then

   -a file
          True if file exists.
   -b file
          True if file exists and is a block special file.
   -c file
          True if file exists and is a character special file.
   -d file
          True if file exists and is a directory.
   -e file
          True if file exists.
   -f file
          True if file exists and is a regular file.
   -g file
          True if file exists and is set-group-id.
   -h file
          True if file exists and is a symbolic link.
   -k file
          True if file exists and its ``sticky'' bit is set.
   -p file
          True if file exists and is a named pipe (FIFO).
   -r file
          True if file exists and is readable.
   -s file
          True if file exists and has a size greater than zero.
   -t fd  
          True if file descriptor fd is open 
          and refers to a terminal.
   -u file
          True if file exists and its set-user-id bit is set.
   -w file
          True if file exists and is writable.
   -x file
          True if file exists and is executable.

string
True if the length of string is non-zero.

string1 == string2
string1 = string2
True if the strings are equal. 
When used with the [[ command, this performs pattern matching 
as described above (see Conditional Constructs).

‘= should be used with the test command for POSIX conformance.

string1 != string2
True if the strings are not equal.

string1 < string2
True if string1 sorts before string2 lexicographically.

string1 > string2
True if string1 sorts after string2 lexicographically.

arg1 OP arg2
OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. 
These arithmetic binary operators return true if arg1 is equal to,
not equal to, less than, less than or equal to, greater than, 
or greater than or equal to arg2, respectively. 
Arg1 and arg2 may be positive or negative integers. 
When used with the [[ command, Arg1 and Arg2 
are evaluated as arithmetic expressions (see Shell Arithmetic).

format date-time

date ='%Y-%m-%d %H:%M:%s'

Here is more common date time formats:

Parameter Output
date +”%m/%d/%Y” 03/25/2019
date +”%d-%b-%Y” 25-Mar-2019
date +”%Y %b %m” 2019 Mar 25
date +”%H:%M” 14:40
date +”%I:%M %p” 02:40 PM
date +”%H:%M:%S” 14:40:32
date +”%I:%M:%S %p” 02:40:32 PM
date +”%m/%d/%Y %H:%M” 03/25/2019 14:40
date +”%A, %m %d %Y %H:%M” Monday, 03 25 2019 14:40
date +”%A, %b %d, %Y %I:%M %p” Monday, Mar 25, 2019 02:40 PM
date +”%A, %b %d, %Y %H:%M:%S” Monday, Mar 25, 2019 14:40:32

How to Get Current Date and Time in Bash Script >>


sub string replace

#!/bin/bash

original_string="Hello, World!"
substring_to_replace="World"
replacement="Universe"

new_string="${original_string//substring_to_replace/replacement}"

echo "Original String: $original_string"
echo "New String: $new_string"

Bash Script to Replace one Substring with Other String >>


без грамотической ошибки я русской речи не люблю

факты примеры истории

Tables Are Cool
col 1 is left-aligned $1600
col 2 is centered $12
col 3 is right-aligned $1
factor 100 hits 1000 hits 100K hits 1M hits 10M hits
0 104 255 255 255 255
1 18 49 255 255 255
10 10 18 142 255 255
100 8 11 49 143 255

up

Skillbox Media

Раздел «Код»

users

visudo
adduser newuser
usermod -aG sudo newuser

useradd -r -u 150 -g mail -d /var/vmail -s /sbin/nologin \
       -c "Virtual Mail User" vmail
# -r, --system                  create a system account

cat /etc/passwd

Understanding the /etc/passwd File

printf

Character Usage
%s String
%c Single character
%d Integers
%o Octal integers
%x Hexadecimal integers
%f Floating point
%b String with backslash escape character
%% Percent sign
printf '%5s |%5s|%-5s|%-20.5f| %.5s|%5s \n' \
    1 2 3 4444,4444 kkkkhhh 6

Using Bash printf Command for Printing Formatted Outputs

awk

awk -F':' '{printf "%-20s %s\n",$1,$3 }' /etc/passwd

How To Use awk In Bash Scripting

tar gz zip

tar -xvf файл.tar # extract
tar -xvzf файл.tar.gz # extract
unzip file.zip  # extract

tar options

Options Description
-c Creates an archive by bundling files and directories together.
-x Extracts files and directories from an existing archive.
-f Specifies the filename of the archive to be created or extracted.
-t Displays or lists the files and directories contained within an archive.
-u Archives and adds new files or directories to an existing archive.
-v Displays verbose information, providing detailed output during the archiving or extraction process.
-A Concatenates multiple archive files into a single archive.
-z Uses gzip compression when creating a tar file, resulting in a compressed archive with the ‘.tar.gz’ extension.
-j Uses bzip2 compression when creating a tar file, resulting in a compressed archive with the ‘.tar.bz2’ extension.
-W Verifies the integrity of an archive file, ensuring its contents are not corrupted.
-r Updates or adds files or directories to an already existing archive without recreating the entire archive.

htop top

jobs bg fg

top 
ctrl+z
htop
ctrl+z
jobs
bg %1
fg

rename by user name

usermod -l login-name old-name # change login of username

rename by a UID (user ID):

usermod -u UID username # change uid of username

List all users

cat /etc/passwd
cut -d: -f1 /etc/passwd

You need to kill all Linux process owned by user named tom and forcefully logged them out of the system:

pkill -u tom pid
pkill -9 -u tom
usermod -l jerry tom # change name of user tom
groupmod -n jerry tom # change name of group tom

How to change user tom UID from 5001 to 10000

Type the usermod command as follows:

id tom
usermod -u 10000 tom # change uid of login
id tom

How to change user home directory from /home/tom/ to /home/jerry

The syntax is as follows:

usermod -d /home/jerry -m jerry
id jerry
ls -ld /home/jerry

Linux Change or Rename User Command Syntax

addgroup

groupadd vmail
Usage: groupadd [options] GROUP
Options:
  -f, --force                   exit successfully if the group already exists,
                                and cancel -g if the GID is already used
  -g, --gid GID                 use GID for the new group
  -h, --help                    display this help message and exit
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -o, --non-unique              allow to create groups with duplicate
                                (non-unique) GID
  -p, --password PASSWORD       use this encrypted password for the new group
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       directory prefix
      --extrausers              Use the extra users database


Usage: groupmod [options] GROUP
Options:
  -g, --gid GID                 change the group ID to GID
  -h, --help                    display this help message and exit
  -n, --new-name NEW_GROUP      change the name to NEW_GROUP
  -o, --non-unique              allow to use a duplicate (non-unique) GID
  -p, --password PASSWORD       change the password to this (encrypted)
                                PASSWORD
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files


groupadd -g 2000 vmail # add group with groupID
groupmode -g 2000 vmail # change gid of groupname
groupmode -n vmail2 vmail # change groupname of groupname
usermode -u 2000 vmail # change uid of username
usermode -g 2000 vmail # change group id of username

change groups of postfix and dovecot

/etc/dovecot/dovecot-sql.conf.ext:user_query = \
    SELECT maildir, 150 AS uid, 8 AS gid FROM mailbox \
    WHERE username = '%u' AND active = true

$:~# grep -r 150 /etc/postfix
/etc/postfix/main.cf:virtual_uid_maps = static:150
/etc/postfix/main.cf:virtual_gid_maps = static:150
grep -r vmail -A 10 -B 10 /etc/dovecot /etc/postfix
history | grep -E 'nano|cd'
ls | xargs cat
ls | xargs -I % sh -c 'echo ===================; cat %; '
ls | xargs -I % sh -c 'cat ./%; echo ===================;'

grep -r Ln -A 5 -B 5 /etc/dovecot /etc/postfix
grep -rn Ln  /etc/dovecot /etc/postfix
grep -rn Ln -A 5 -B 5 /etc/dovecot /etc/postfix
/etc/dovecot/conf.d/10-director.conf:25:# %Ln
grep -rn %u  /etc/dovecot /etc/postfix

nano /etc/dovecot/conf.d/10-auth.conf
# set %Ln unstil %n
systemctl reload dovecot

grep

поиск по файлу

# remove commented and empty line
grep -v '^//\|^$' defaults.inc.php

Grep OR – Grep AND – Grep NOT – Match Multiple Patterns

diff

сравнение файлов

diff [options] file1 file2 -y --color='auto'
diff ~/.bash_aliases ~/.bash_aliases_dmp -y --color='auto' -q

-y, --side-by-side output in two columns
-q will only tell you that the files “differ”. If they do not, you will get no output.

Using Diff Command to Compare Two Files in Linux Terminal
diff(1) — Linux manual page

ubuntu: ввод спец символов с клавиатуры

You can also press the Ctrl+Shift+u keyboard combination which will print a u character on the screen. Then after the u type one of the 4-digit hexadecimal codes in the second column and press Enter to render the u+4-digit hexadecimal number that appears on the screen as the appropriate arrow character.

Character Hex HTML Entity
2190 &larr;
2191 &uarr;
2192 &rarr;
2193 &darr;
2194 &harr;
2195 &varr;
2196 &nwarr;
2197 &nearr;
2198 &searr;
2199 &swarr;

typing arrows in plain text

Big-O

Big-O Cheat Sheet

IMGS - конвертация и размер

sudo apt-get install imagemagick
pwd
ll
# resize and convert
convert poster_top.png -resize 1300x312 poster_top.jpeg

# rotate
convert image1.jpg -rotate 90 image1-rotated.jpg
for file in *.png; do convert $file -rotate 90 rotated-$file; done

#crop
convert image.jpg -crop 703x470+3+5 output.jpg

identify poster_top.jpeg

# ---------
$ convert apande.ru_2.png  apande.ru_2.jpeg
$ convert apande.ru_2.jpeg -resize 1980x1980 apande.ru_2_r1.jpeg
$ identify apande.ru_2_r1.jpeg
apande.ru_2_r1.jpeg JPEG 757x1980 757x1980+0+0 8-bit sRGB 332053B 0.000u 0:00.000

askubuntu.com: convert images to other formats
imagemagick.org: Command-line Tools: Convert

watch stat

Наблюдение за файлом и обновление данных
file 'watch.sh':

pwd
ll -al 2.py


f=2.py

t=0
w=`pwd`
c="stat ${f} -c %Z"
ts="pytest $f"
st1=`$c`
while true
do

sleep 1
st=`stat $f -c %Z`
if [[ $st1 != $st ]]; then
echo
stat ${f} -c %z
echo $st1
$ts
st1=$st
echo '=============================='
echo '=============================='
fi
# ((t++))
# echo ${t} $st $st1
# echo $w
done

run shell:

$ . watch.sh

while loop

a=10;b=1;w='>';while [[ "$a" -ge 10 ]] && [[ "$a" -le 100 ]] ; do echo $a $b $w;a=$(($a+b)); if ((a==100)); then b=-1;w='<';fi;if ((a==10));then b=1;w='>';fi;sleep .05; done

or

a=10
b=1
w='>'
while [[ "$a" -ge 10 ]] && [[ "$a" -le 100 ]]
do
echo $a $b $w
a=$(($a+b))

if ((a==100))
then
b=-1
w='<'
fi

if ((a==10))
then
b=1
w='>'
fi

sleep .05
done
Ctrl + S : Update