Bash
Bash commands
if: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
vi: http://www.infobound.com/vi.html
sudo: http://fedorasolved.org/post-install-solutions/sudo
arithmetic expressions: http://www.softpanorama.org/Scripting/Shellorama/arithmetic_expressions.shtml
functions: http://tldp.org/LDP/abs/html/functions.html
List disks: lsblk
List disk id: blkid
Auto mount disk in Debian: https://superuser.com/questions/646249/automatically-mount-a-2nd-hard-drive-in-debian-7
Disk statistics: dstat
File and folder security: http://www.unix.com/tips-tutorials/19060-unix-file-permissions.html
filename=/tmp/filename-$
$$ can be used to create unique filenames. In Bash $$ will be the PID of the process, and therefore would be different for each instance of a program.
–
Rename extensions on all files (also ok with spaces in filename). If you want the same name you have to move the files to another folder, therefore I’ve added “.1” before the file extension in the example below:
for f in *.JPG; do mv "$f" "`basename "$f" .JPG`.1.jpg"; done;
File name manipulation within Bash scripts:
ext=${file##*.} basename=${file##*/} basenamewithoutext=`basename "$file" .$ext` dirname=`dirname "$file"` dirname=${file%/*}
List all lines in a text file and their line number:
N=0 while read LINE ; do N=$((N+1)) echo "Line $N = $LINE" done < my.file
List files using open incoming TCP ports:
sudo lsof -i
Check the date of a file:
date -r $file +%F
Show the first column using separator char “%”:
cut -d "%" -f1
grep -lir "some text" *
# ls -R /var | grep -v DoNotIncludeThis | grep [[:upper:]]
The -l switch outputs only the names of files in which the text occurs (instead of each line containing the text), the -i switch ignores the case, and the -r descends into subdirectories.
sudo find / -type f -perm -a+x -name *ffmpeg*
Convert all files in a folder to WAV using ffmpeg:
find "/mnt/nas/MP3/Kesha/" -type f|while read foo; do echo ffmpeg -y -i \"$foo\" \"${foo##*/}.wav\" >> convert.sh; done
Set x-flag on all directories (works in Busybox, Q700, QNAP TS-219 and more):
find . -type d|while read foo; do chmod 705 "$foo"; done or (works in Ubuntu): sudo find /path/to/someDirectory -type d -print0 | xargs -0 sudo chmod 755
find /etc/config/ -mtime 0 -type f |xargs ls -l
while read file do find "/media/recordings/active_campaigns/" -ctime -15 -name "$file*" -exec cp -v "{}" /media/recordings/temp2 \; echo "$file" >> /home/temp/moved_$(date +%d_%m_%Y).txt done < /media/recordings/move.txt
last last -a lastlog
List TCP ports used by applications:
sudo netstat -lp sudo netstat -p
Lista kataloger sorterat på datum med valfria kolumner
ls -lt | grep '^d' | awk '{ print $8 " " $6 }'
Ref: http://www.cyberciti.biz/faq/linux-list-just-directories-or-directory-names/
Tidsstämpla en MySQL backup
mysqldump --defaults-extra-file=/home/my_server/backup/my.cnf db_name | \ gzip > "/home/my_server/backup/mysqlbu_db_name_`date +%m%d%Y`.tar.gz" \ 2>>"/home/my_server/backup/logs/Errorlog.txt"
Kolla om datorn har ny IP
Kör följande skript via crontab:
#!/bin/sh fileip=/tmp/oldip.txt touch $fileip oldip=`awk '{printf$1}' $fileip` newip=`/sbin/ifconfig ppp0|awk '{if(NR==2)printf$2"\n"}'` if [ "$newip" != "$oldip" ]; then echo $newip|mail -s ip yourname@yourdomain.com echo $newip > $fileip fi
Kolla webbsida efter ord och maila
Följande script kollar om en sida innehåller ordet “Offline” och mailar i så fall:
#!/bin/bash wget http://domain.com/file.html grep -w Offline file.html if [ $? -eq 0 ]; then mailx -s "Offline found" user@yourdomain fi rm file.html
Ett alternativt skript som gör samma sak:
#!/bin/bash alert_string="offline" alertme() { mailx -s "Offline found" user@yourdomain } wget -t 1 -T 5 --quiet -O - "http://yourdomain.com" 2>/dev/null | \ grep -i "$alert_string" && alertme
The “-t” argument to wget is the number of tries it does before exiting, if you do not set this param, I think wget tries several times (which may be desired), the “-T” is the time out time in secons, if you do not specify this and for some reason the page is not reachable, wget may block your script for some predefined default wich I think is 45 secs.
You can specify whathever you want in the function “alertme”.. personally I use sendmail for such operations, but any other program may work..
alert() { echo -e "Subject: Offline Alert " | sendmail -falert@xpro.se user@domain & }
Auto mount a NAS
#!/bin/bash # # Check and connect to the NAS /Tobias Holm 2011-05-17 http://xpro.se # Tested in Ubuntu 10.10 LOG=/tmp/CheckNAS.log #--- Verify the destination is reachable --- DESTOK=`ping -c 1 -q nas |awk '{if ($6=="0%") printf "ok"}'` LOGTIME=$(date +"%Y-%m-%d %H:%M:%S %Z") if [ "$DESTOK" == "" ]; then echo "[$LOGTIME] NO PING RESPONSE FROM DESTINATION! EXITING..." | tee -a $LOG exit 1 else echo "[$LOGTIME] PING RESPONSE OK" | tee -a $LOG fi #--- Mount the NAS if a certain file can't be read --- LOGTIME=$(date +"%Y-%m-%d %H:%M:%S %Z") if [ ! -f /mnt/nas/nosync.txt ]; then echo "[$LOGTIME] MOUNTING NAS..." | tee -a $LOG mount /mnt/nas fi #--- Check NAS --- LOGTIME=$(date +"%Y-%m-%d %H:%M:%S %Z") if [ ! -f /mnt/nas/nosync.txt ]; then echo "[$LOGTIME] NAS DOWN!" | tee -a $LOG else echo "[$LOGTIME] NAS OK!" | tee -a $LOG fi
Save the script in your home folder in a subfolder called ‘code’ and make it executable. Then you can run this script every second minute with cron:
crontab -e
Add the following line at the end of your crontab file:
*/2 * * * * sh $HOME/code/CheckNAS.sh
Arrayer i Bash
#!/bin/bash ###set -x for RA in array1 array2 do INDX=0 while [ ${INDX} -le 10 ] do eval "${RA}[${INDX}]=`expr ${INDX} \* ${INDX}`" (( INDX++ )) done done for RA in array1 array2 do INDX=0 while [ ${INDX} -le 10 ] do eval echo '${'"${RA}[${INDX}]"'}' (( INDX++ )) done done exit 0
Rename all files to remove the extension
This script will rename all files with an extension like “file.mp3” to “file”.
#!/bin/bash files=$(ls) for file in $files do if [[ $file != ${file.*} then mv -f $file ${file%.*} fi done
Check text in mail and delete it
It would be cleaner to use a mail program to read and delete the mail, but if that is not possible, the folowing script will delete the mail with the specified text in it.
It assume that $MAIL is the file containing that user’s mail, and that the mail follows standard mailbox rules; each mesage starts with a From line, and any lines starting “From ” within the mail are escaped (which is why you sometimes see “>From ” within an email). The space after the From is important – there may be one or more “From: ” lines, with a colon in them, but we aren’t looking for those. The “checkline” variable contains the text which will uniquely identify the test email.
Before you test this, copy the user’s $MAIL file somewhere safe!
Note that there is no locking here – if another mail arrives while the check mail is being deleted, that new mail may be lost.
I do make use of a quirk of csplit here – since the first line of $MAIL starts “From “, csplit will create an empty first file, and the second file it creates will contain the first email. This means that even if all emails (or the only email) in $MAIL is deleted, the “cat” below the “rm” line still has at least one file (i.e. the empty first file) to write to $MAIL.
checkline="This is the test email" if grep "$checkline" $MAIL >/dev/null then # test email has arrived - delete it mkdir /tmp/check.$ oldpwd=$(pwd) cd /tmp/check.$ csplit $MAIL -n 4 -k -s '/^From /' '{*}' rm $(grep -l "$checkline" *) cat * > $MAIL cd "$oldpwd" rm -r /tmp/check.$ else # No check email found - tell someone echo No Check Email on $(date) | mail -s "No check email today" adminuser@wherever.com fi
FTP script with SFTP
#! /bin/bash v_rundir="/home/abc" v_ftp=`cat ftp.log | grep "not found" | wc -l` cd $v_rundir rm ftp.log # Test to see if this process is already running... if [ -e process.lck ] ; then # if it is, then exit... exit fi echo $ > process.lck # otherwise, lock this process from duplicated execution... #FTP Function # Get the new pgp files... sftp username@ftp4.server.com <<EOF >>/home/abc/ftp.log 2>&1 cd abc/Toabc mget abc*.txt.pgp rm abc*.txt.pgp quit EOF if [ $v_ftp -ne "0" ]; then rm process.lck exit 1 fi exit
Recent Comments