Pass a bash array as an argument

I have a bash script in which I’m using an array to get a list of files and I needed to pass the content of this array as an argument, to send an email to some users.

This is the array:

RAW_LIST="$(echo "ls -1 /download")"
declare -a LIST
readarray -t LIST <<<"${RAW_LIST}"

This is how I passed the array to bash:

printf %"s\n"  "${LIST[@]:1}" | /bin/mail -s "$SUBJECT" "$EMAIL"

Trailing :1 in the array will consider all the elements after element [0] as, in this specific case, it'd be ls -1, pretty useless sending it to the user.

Script: receive files via SFTP

In the past days I had to write a script to download files from a remote server using an SFTP connection; this script will be run by cron.

The goal was to download the files on my server and delete them on the remote machine after being downloaded.

I used an array to get the list of the current files on the remote server and a for-loop to download them.

#!/bin/bash
# 
# 20180801
#
# Receive files via SFTP


LOGFILE=/var/tmp/receivefiles.$(date -I).log
echo "START: $(date)" >>$LOGFILE

# Connection data
HOST='XXX.XXX.XXX.XXX'
PORT='22'
USER='mashiny'

# Where to download files
cd /home/mashiny/download

# File list
LIST_RAW="$(echo "ls -1 /out" | sftp -oPort=$PORT $USER@$HOST 2>/dev/null)"
declare -a LIST
readarray -t LIST <<<"${LIST_RAW}"

if   [[ "${#LIST[@]}" -lt 1 ]]; then
    echo "Unknown error in SFTP connection" >>$LOGFILE
    exit 1
elif [[  "${#LIST[@]}" -lt 2 ]]; then
    echo "No files to download" >>$LOGFILE
    exit 0
fi

# Connection via SFTP and download files
for FILE in "${LIST[@]:1}"
do
    # Download
    echo "get $FILE" | sftp -oPort=$PORT $USER@$HOST &>/dev/null
    [ $? -ne 0 ] && echo "$FILE download failed" >>$LOGFILE && continue
    echo "$FILE downloaded" >>$LOGFILE

    # Delete on remote dir
    echo "rm $FILE" | sftp -oPort=$PORT $USER@$HOST &>/dev/null
    [ $? -ne 0 ] && echo "$FILE not removed" >>$LOGFILE && continue
    echo "$FILE removed" >>$LOGFILE
done

echo "END:   $(date)" >>$LOGFILE
exit 0

Check software RAID script

Here’s a Bash script to check software RAID that will send an e-mail reporting the array status:

#!/bin/bash
# check_raid.sh
EMAIL="your@email.com"

if egrep "\[.*_.*\]" /proc/mdstat  > /dev/null
 then
   logger -s "mdcheck: RAID devices ERROR"
   echo "Software RAID devices ERROR on ${HOSTNAME}" | /bin/mail -s \
   "$0: Software RAID devices ERROR on ${HOSTNAME}" ${EMAIL}
 else
   logger -s "mdcheck: RAID devices OK"
   echo "Software RAID devices OK on ${HOSTNAME}" | /bin/mail -s \
   "$0: Software RAID device OK on ${HOSTNAME}" ${EMAIL}
fi

 

Nagios plugin: monitor a systemd service

Here’s a tiny plugin to check if a systemd service is running:

#!/bin/bash
#
# машины
# 
# Check a if a systemd service is running
# 
# Usage: $0 
#
#Nagios exit codes
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3

SERVICE=$1

# Check service status

systemctl -q is-active $SERVICE
if [[ $? -ne 0 ]]; then
    echo "ERROR: service $SERVICE is not running"
    exit $CRITICAL
fi

echo "OK: service $SERVICE is running"
exit $OK

Flushing iptables

You can flush and reset iptables to default running these commands:

iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

The -F command flushes all the chains and -X deletes empty (non-default) chains.
You can also create a script:Continue reading