0

I have a text file b.txt with the following text:

notify-send -i gtk-info "How Are You" -h string:x-canonical-private-synchronous:anything

I have a shell script with the following code:

#!/bin/sh

while true;
do if [ -s b.txt ]
then
value="$(cat b.txt)"
exec $value
sleep 0.1
fi
done

When I run the script it throws the following error and notification is not displayed:

Invalid number of options.

But when the text file contains the command with just two words like this:

notify-send -i gtk-info "Hows You" -h string:x-canonical-private-synchronous:anything

the notification is displayed perfectly.

This issue is happening only when I execute notify-send through a shell script. How do I display a notification with a message having any number of words?

node_man
  • 1,359
  • 4
  • 23
  • 50

2 Answers2

2

exec is entirely the wrong thing to use here. Maybe try

#!/bin/sh

while true;
do
  if [ -s ./b.txt ]
  then
    . ./b.txt
    sleep 0.1
  fi
done

But running this in a tight loop seems like an odd thing to want to do, and executing a file of unknown provenance is dubious at best. Are you sure this text file will always contain exactly the contents you expect and cannot be modified by anyone? Why is this command being put in a separate file in the first place?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • ./b.txt: Text file busy it gives this error sometimes – node_man Jan 31 '19 at 07:45
  • the file is being generated by a docker container. Since docker doesn't support notify-send so I'm using this method to write the command to txt file and execute it on host – node_man Jan 31 '19 at 07:45
  • 1
    `docker exec whatever | sh` would avoid the temporary file and associated problems. The "text file busy" indicates that Docker hasn't (yet?) closed the file. This still seems like a rather ... peculiar design. – tripleee Jan 31 '19 at 07:48
1

Try replacing exec $value with eval $value

nullPointer
  • 4,419
  • 1
  • 15
  • 27
  • While this might perhaps fix the immediate problem, it's generally dubious advice. At the very least, you should have some caveats. – tripleee Jan 31 '19 at 07:47
  • @tripleee this solution seems to work perfectly without giving any issues – node_man Jan 31 '19 at 07:59