141

I am new to bash script programming.

I want to implement a bash script 'deploymLog', which accepts as input one string argument(name).

[root@localhost Desktop]# ./deploymLog.sh name

here I want to pass the string argument(name) through command line

As an initial step, I need to append the current timestamp along with this input string to a log file say Logone.txt in current directory in the below format:

[name]=[System time timestamp1]

How it is possible?

0

3 Answers 3

137
$> cat ./deploymLog.sh 
#!/bin/bash

name=$1
log_file="Logone.txt"

if [[ -n "$name" ]]; then
    echo "$1=$( date +%s )" >> ${log_file}
else
    echo "argument error"
fi

The first argument from a command line can be found with the positional parameter $1. [[ -n "$name" ]] tests to see if $name is not empty. date +%s returns the current timestamp in Unix time. The >> operator is used to write to a file by appending to the existing data in the file.

$> ./deploymLog.sh tt

$> cat Logone.txt 
tt=1329810941

$> ./deploymLog.sh rr

$> cat Logone.txt 
tt=1329810941
rr=1329810953

For more readable timestamp you could play with date arguments.

2
  • 9
    total beginner here... it would be helpful to know what the [[ -n "$name" ]] part is doing. Jun 13, 2016 at 16:14
  • 5
    The "[[ -n "$name" ]]" is another form of the "test" command. See: ss64.com/bash/test.html
    – jewettg
    Aug 1, 2016 at 19:44
75

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).

9
#!/bin/bash

name=$1

echo "$(date '+%Y%m%d-%H:%M:%S') => " $name >> x.log

run "bash deploymLog.sh whatever", and you got x.log with

20120220-23:53:50 =>  whatever
3
  • I suppose the downvoter missed the reason of name=$1. The variable $name is never used, as you output only the string 'name' literally.
    – manatwork
    Feb 21, 2012 at 15:24
  • @DynoHongjunFu Still, the variable has same name and value, which is not a good way to make the example readable. Oct 26, 2014 at 15:06
  • It is best to quote the var when assigning to name like this: name="$1"
    – Jake
    Apr 9, 2015 at 18:03

You must log in to answer this question.