115

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?

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

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

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

  • 7
    total beginner here... it would be helpful to know what the [[ -n "$name" ]] part is doing. – MichaelChirico Jun 13 '16 at 16:14
  • Yup, that^s true I'm also a total noob and my script is dying on that line??? – pythonian29033 Jul 15 '16 at 6:38
  • 4
    The "[[ -n "$name" ]]" is another form of the "test" command. See: ss64.com/bash/test.html – jewettg Aug 1 '16 at 19:44
64

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

7
#!/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
  • 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 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 '14 at 15:06
  • It is best to quote the var when assigning to name like this: name="$1" – Jake Apr 9 '15 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy