GNU/Linux


Bash: Check if file is found and contains data

The following code will check if the file described by variable FILENAME exists and has data.

1
2
3
4
5
6
7
8
9
10
11
12
13
if [ ! -f "$FILENAME" ]; then
 
    echo "Error: '$FILENAME' file not found.";
else
 
    if [ ! -s "$FILENAME" ]; then
 
        echo "Error: '$FILENAME' file is empty.";
    else
 
        echo "Info: '$FILENAME' file found and contains data.";
    fi
fi

Convert a list of integers from MySQL to a Bash array

The following code will connect to a MySQL server, it will get a list of integers and convert the results to a bash array that will be iterated using a for loop and they will be printed using zero padding.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
IDS_RAW=$(mysql --user="myuser" --password="mypass" --host="db.example.com" --database="mydb" --batch --compress --skip-column-names --execute="
  SELECT
    Id
  FROM
    Users
  WHERE
    Status = 0;
");
 
OLDIFS=$IFS;
IFS=$'\n' command eval;
'IDS=($IDS_RAW)';
IFS=$OLDIFS;
 
echo "Will process the following user IDs '${IDS[@]}'";
 
for ID in "${IDS[@]}"; do
  LEADING_ZERO=$(printf %08d $ID);
  echo "ID $LEADING_ZERO";
done;

Bash: Get date N days ago in the format YYYY_MM_DD

1
2
3
DEFAULT_DAYS_BACK="0";
DAYS_BACK=${1:-$DEFAULT_DAYS_BACK};
PROCESSING_DATE=`date --date="$DAYS_BACK day ago" +%Y_%m_%d`;

The above code will populate variable PROCESSING_DATE with the date of the day the was DAYS_BACK ago using the format YYYY_MM_DD.

The value for DAYS_BACK is filled by the first argument of the script/function $1. In case the user did not provide an argument, we have the variable DEFAULT_DAYS_BACK that will be used as the default value.


Bash get script file name and location 1

The following code will populate the variables SCRIPT_NAME and SCRIPT_DIR with the name of the script currently being execute and the location this script is in:

1
2
SCRIPT_NAME=$(basename $(test -L "$0" && readlink "$0" || echo "$0"));
SCRIPT_DIR=$(cd $(dirname "$0") && pwd);

Notes for SCRIPT_NAME:

  • $0 expands to the name of the shell or shell script
  • test -L "$0" checks that input is a file that exists and is a symbolic link
  • && readlink "$0" will be executed if the above statement is true and it will print the resolved symbolic link
  • || echo "$0" will be executed if the test for symbolic link fails
  • finally, basename will strip directory and suffix from whatever is returned from the above statements

Notes for SCRIPT_DIR:

  • Will not resolve the correct folder if the last component of the path is a symbolic link (symlink). It will return the location of the symlink instead of the location of the file the symlink is pointing to
  • cd will return 0 if it successfully navigates to a directory or 1 when it fails to navigate to the directory
  • cd "$( dirname "$0" )" will use dirname to strip the last component from the expanded name and try to navigate to that location
  • if the above cd fails, we get the current location using && pwd. pwd will print name of current/working directory

In case you have a problem with $0, it is overwritten or the above function is called by a child script in another folder you can replace $0 with ${BASH_SOURCE[0]}.

1
2
SCRIPT_NAME=$(basename $(test -L "${BASH_SOURCE[0]}" && readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}"));
SCRIPT_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd);