bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Variable passed to system contains garbage characters


From: Bob Proulx
Subject: Re: Variable passed to system contains garbage characters
Date: Mon, 17 Dec 2007 13:00:56 -0700
User-agent: Mutt/1.5.13 (2006-08-11)

Patrick Nagelschmidt wrote:
>         The script below fails on my box with the following output:
> 
>         1197676800
>         date: invalid date `1970-01-01 \033[?1034h1197676800 sec'
> 
>         So for some reason the value passed to date got a nasty prefix.

I cannot recreate the problem.  However I think your issue is because
you are forcing bc into interactive mode.

>         #!/bin/bash
>         let "STEP=86400"
>         let "CALCTIME=`date +%s`"
>         export DAYSTART=`echo "$CALCTIME - ($CALCTIME % $STEP) - $STEP" |bc 
> -i |tail -1`

Remove the -i option from bc and see if that solves your problem.  My
version of bc does not produce escape sequences but it appears that
yours does.  You don't want to run bc in interactive mode here
regardless.

>         echo "$DAYSTART"
>         export BASHBUG=`date -d "1970-01-01 $DAYSTART sec" +"%Y-%m-%d %T"`
> 
> Fix:
>         Yes, please ;)

But I don't think this has anything to do with bash.  It is a bc /
scripting problem.

As a hint I would remove the pipe to bc and simply let bash do the
math here.  Try this instead.

  #!/bin/bash
  let "STEP=86400"
  let "CALCTIME=`date +%s`"
  export DAYSTART=$(($CALCTIME - ($CALCTIME % $STEP) - $STEP))
  echo "$DAYSTART"
  export BASHBUG=`date -d "1970-01-01 $DAYSTART sec" +"%Y-%m-%d %T"`

Also if you have a new enough version of coreutils then date can be
simplified too.

  export BASHBUG=`date -d "1970-01-01 $DAYSTART sec" +"%Y-%m-%d %T"`

Plus +%F is a more compact date string.

  export BASHBUG=`date -d "1970-01-01 $DAYSTART sec" +"%F %T"`

A fully updated script.  It only uses standard features and could use
the start #!/bin/sh okay too.

  #!/bin/bash
  STEP=86400
  CALCTIME=$(date +%s)
  export DAYSTART=$(($CALCTIME - ($CALCTIME % $STEP) - $STEP))
  echo "$DAYSTART"
  export BASHBUG=$(date -d "@$DAYSTART" +"%Y-%m-%d %T")
  echo "$BASHBUG"
  exit 0

Bob




reply via email to

[Prev in Thread] Current Thread [Next in Thread]