bash-scripting

1) An example where system sends a mail of the command line:

#!/bin/bash
#### variablen definition anfang #########
A=`ls  /home/motion/motion | wc -l`
#### variablen definition ende #########

### falls die anzahl der bilder ändert sich, dann tu das und das ####
if [[ $A -ne 0 ]]
 then
            echo "alarm" | sudo mail -s "motionserver-kontrole"  test@test.de
else    
            echo "alles OK"
fi

-------------

2) Zwei Skripte um die Bilder zu optimieren

#!/bin/bash
mkdir /tmp/OPT;
for Y in `ls`;do convert  $Y  -resize 1200x900  $Y-opt.jpg ; mv *opt* /tmp/OPT; done;

# convert image.jpg -resize 50% image_new.jpg

3) GIF-Animation

# convert -delay 120 -loop 0 *.jpg GIF-Santa.gif

o reduce a pdf-file (pdf-file compression):

1) the command:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=${1%\.pdf}your_small_file.pdf $1.pdf

You can write that as a script for example:  "compress_pdf"

#!/bin/bash
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=${1%\.pdf}your_small_file.pdf  $1

and then execute:

./compress_pdf  your_original_file.pdf

as a result you get the following in the directory where you are:

your_small_file.pdf

------------

2) to give the command:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=${1%\.pdf}your_small_file.pdf orig_file.pdf

-----------------------

As a result: your original file "orig.pdf" will be your_small_file.pdf

For example:

orig.pdf      - 13,4 MB

your_small_file.pdf  -  519,4 KB

------------------------

3) gs -sDEVICE=pdfwrite -dCompatibilityLevel=4 -dNOPAUSE -dBATCH -r150 -sOutputFile=Zieldatei.pdf

And finally (if you have a lot of pdf-files), you can apply this script:

#!/bin/bash

PWD=pwd

cd $PWD

for A in `ls *pdf`; do gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=${1%\.pdf}optimiert/$A.pdf $A;done

------------------

Head keyword "user"

create user with bash

# useradd -m -d /home/iamauser -s /sbin/nologin  -G users  -c "i am a user" iamauser

user "iamauser" rename to jan

#usermod -l  jan  iamauser

.......................................

user jan new directory "/home/xyz"  to assign

# usermod -d /home/xyz -m jan
.......................................

user jan lock

# usermod -L jan

.............

user jan group sudo attach

# usermod -aG sudo jan

 delete user with home directory

# userdel -r User-Name

# userdel -r jan

------------------

# ls -la big_file

-rw-rw-r--   1 user users  187603950   Jun 14 10:13 big_file

big_file = 179 MB 

I would like to splitting this file, for example into 10 MB files.

splitting:

# split -b 10m big_file  little_file_

Result:

-rwxrwxrwx 1 user user 187603950  Jan 23  2018 big_file
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_aa
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_ab
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_ac
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_ad
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_ae
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_af
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_ag
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_ah
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_ai
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_aj
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_ak
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_al
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_am
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_an
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_ao
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_ap
-rw-rw-r-- 1 user user  10485760  Jan 23 11:32 litle_file_aq
-rw-rw-r-- 1 user user   9346030  Jan 23 11:32 litle_file_ar

 assemble:

cat little_file_* > big_file_result

ls -la big_file_result

-rwxrwxrwx 1 user user 187603950  Jan 23  2018 big_file_result

------------------

kill user (kill bill ;-)

sudo pkill -9 -u bill
pkill -9 -u bill
pgrep -u bill
ps -fp $(pgrep -d, -u bill)

------------------

Transfer the tar-archive via ssh to remote computer

#!/bin/bash
#### variablen definition anfang #########
datum=`date +"%d"``date +%"m"``date +"%y"`
#### variablen definition ende #########
tar -czf - /home/motion/  | ssh ferner-rechner  "cat > /home/$datum-bilder.tgz"

or in a line (without script)

# tar -cvf - /home/user123/ | ssh hostname  "cat > /tmp/user123.tar"

---------------

show "pure" ip-address (as numeral)

# ifconfig | head -2 | tail -1 | awk -F: '{print $2}' | awk -F\  '{print $1}'

PS. Sometimes we are need (in scripts) only numeral number

-----------------

kill all processes as root

you must be root!

# echo k > /proc/sysrq-trigger

--------------

if cursor disappeared (is gone) (falls mauszeiger weg ist)

1) # gsettings set org.gnome.settings-daemon.plugins.cursor active false

---------------
2) # sudo modprobe -r psmouse

# sudo modprobe psmouse
---------------
3)  # xdotool mousemove 100 100

---------------------------

gnome console completely restarting:

# sudo service gdm restart

------------

show soundcard

# cat /proc/asound/cards

---------------------------

sound test: aplay

#  aplay /usr/share/sounds/alsa/Front_Center.wav

------------------------

check sound on the terminal

check sound -  the simple method on the terminal (for Linux Mint):

# aplay /usr/share/sounds/alsa/Front_Center.wav

if you hear words "front senter", then everything is ok!

ALSA: Advanced Linux Sound Architecture

Check it if if ALSA recognized your sound card:
# cat /proc/asound/cards
output (for example):

0 [Intel          ]: HDA-Intel - HDA Intel
                      HDA Intel at 0xfe6fc000 irq 26
 1 [VF0770         ]: USB-Audio - Live! Cam Sync HD VF0770
                      Creative Technology Ltd. Live! Cam Sync HD VF0770 at usb-0000:00:1d.7-4, high s

if output say:

"no soundcards"

then:

# lspci | grep -i audio

output (for example):

# 00:1b.0 Audio device: Intel Corporation NM10/ICH7 Family High Definition Audio Controller (rev 01)

if your system does not provide any output,  then you have bad luck, respectively have you bad sound cards :-(

-------------

show linux version

# lsb_release -a

# cat  /etc/issue

---------------------------

f you have American keyboard and want to change to others, then:

# sudo apt-get install console-data
# sudo dpkg-reconfigure keyboard-configuration
# sudo apt-get update