0

I have a simple bash script below

#!/bin/bash
sratio=`/root/trap -t -a | awk -F '|' '{print $6}' | sed -e 's/[\t ]//g;/^$/d' | sed '1,2d' | awk '{n += $1}; END{print n}'`
tdisks=`/root/trap -t -a | grep "/dev/sd" | wc -l` 

result=$(echo "scale=4; $sratio / $tdisks" | bc)
echo "$result" > /root/tratio.txt

The above script executes fine, when it is run in the terminal, however whenever I put in the cron it fails. Please note trap is a binary file and the command outputs are stored as variables i.e. sratio and tdisks. The sample outputs of sratio (floating) and tdisks (integer) are "23.567" and "80".

* * * * * /tmp/ratio.sh > /tmp/log.txt 2>&1

The error I could see is below in the logs. Please let me know how can i solve this issue.

standard_in 1 syntax error

xrkr
  • 189
  • 7
  • 1
    What is `cmd`? Does it expect to read from standard input? Jobs run by `cron` don't have standard input. See https://unix.stackexchange.com/a/345985/22257 – chepner Mar 15 '23 at 15:32
  • HI .. My apologies for not being clear.. cmd is a command and the output of the command is stored in a variable. – xrkr Mar 15 '23 at 15:37
  • 3
    *Which* command is the important question. It's the only part of your script that might care if standard input is available. – chepner Mar 15 '23 at 15:41
  • 1
    From what I see, the error message is most likely produced by the command `cmd`. – user1934428 Mar 15 '23 at 16:36
  • You may be interested in [this](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quoting-characters-in-arguments-passed-to-it-through-varia) as a potential duplicate. If `cmd` outputs shell syntax characters then they will be treated as literal data when you expand the variable. See also [BashFAQ/050](http://mywiki.wooledge.org/BashFAQ/050) for alternative ways to manage arguments. – tjm3772 Mar 15 '23 at 16:45
  • yes, it seems.. I tried to redirect the output to a text file.. It is empty. BTW>. I edited the question with full command.. trap is a binary file. – xrkr Mar 15 '23 at 16:46
  • 1
    That error message looks like it's coming from `bc`. Check che values of `$sratio` and `$tdisks` to make sure they're what you expect. Putting `set -x` at the beginning of the script will show them in the execution trace. – Gordon Davisson Mar 15 '23 at 19:57
  • Okay. This the command issue which is having path problem. Thanks guys for all the support – xrkr Mar 16 '23 at 11:04
  • Which cron table do you use? The system tables need an additional 6th parameter (username). – Wiimm Mar 17 '23 at 21:48

0 Answers0