Linux Bash scripting: Mixed topics


Debugging

we've had a problem here:
[soshsc show=”logged” role=”administrator”] ... [/soshsc]
invalid attribute(s) a/o value(s)

Let’s assume we have a bash script like this:

test.sh:

#!/bin/bash

fileName="test.txt"

if [ -f $fileName ]; then
  echo "File found"
else
  echo "File not found"
fi

If we run this script with bash command:

bash ./test.sh

We get for example the following result, if the file does not exist:

File not found

But with the help of -x option we can add a bit of debugging and see a bit more what the script is doing:

bash -x ./test.sh

+ fileName=test.txt
+ '[' -f test.txt ']'
+ echo 'File not found'
File not found

Date and time

Generating a string with date and time

now=date +"%Y-%m-%d_%H-%M-%S"
echo "${now}"