Shell command line arguments are accessible via $1 (the first), $n (the nth), or $* (all arguments), so your script should start:
#!/bin/bash
if [ $# -ne 1 ]; then
echo $0: usage: myscript name
exit 1
fi
name=$1
Now the name argument is accessible from the script as $name.
To get the timestamp use the date(1) command and give it a format specifier so it produces the format you want:
now=$(date +%Y%m%d%H%M%S)
Now $now contains the current date and time.
So you can create your log file thus:
logfile=/path/to/log/file/mylogfile.$now
echo "[$name]=[$now]" >> $logfile
You are better off using a shell function to log your messages as it will be easier to use:
function logit
{
now=$(date +%Y%m%d%H%M%S)
echo "$now: $*" >> $logfile
}
Note that shell functions access their own arguments in the same way as the script (via $1 etc.)
So the initial script looks like this:
#!/bin/bash
function logit
{
now=$(date +%Y%m%d%H%M%S)
echo "$now: $*" >> $logfile
}
if [ $# -ne 1 ]; then
echo $0: usage: myscript name
exit 1
fi
name=$1
now=$(date +%Y%m%d%H%M%S)
logfile=/path/to/log/file/mylogfile.$now
logit name = $name
(note the log file isn't in the exact format you specified; it's in a better one with the timestamp at the start of each line).
[[ -n "$name" ]]part is doing. – MichaelChirico Jun 13 '16 at 16:14See: http://ss64.com/bash/test.html – jewettg Aug 01 '16 at 19:44