Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Feb 21 '12 at 14:48

This question came from our site for professional and enthusiast programmers.

3 Answers 3

$> 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

First argument from a command line could be founded with $1. date +%s returns current timestamp in unix time. >> is an operator to append writing to 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.

share|improve this answer
#!/bin/bash

name=$1

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

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

20120220-23:53:50 =>  name
share|improve this answer
    
when you vote down, give a reason, thanks. –  Dyno Hongjun Fu Feb 21 '12 at 8:17
    
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 '12 at 15:24
    
oh, my bad. thanks for pointing out. i need to be more careful. –  Dyno Hongjun Fu Feb 23 '12 at 15:39
    
@DynoHongjunFu Still, the variable has same name and value, which is not a good way to make the example readable. –  Volker Siegel Oct 26 at 15:06

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.