0

Trying to run this code in a bash script however having no luck as it is failing to run. What it is essentially doing is taking the 3rd attribute from the first bash script ${3:-365} if that attribute is not found it uses the default of 365 for the number of days to run the command. then it calls the bash script bash /home/corey/emailSignature/deleteUser.sh ${1} in which is passes a user-defined variable from the first bash script.

at now + ${3:-365} days -f 'bash /home/corey/emailSignature/deleteUser.sh ${1}' &

Is anyone able to let me know what I am doing wrong?

MrToast
  • 21
  • 9
  • How exactly is this "failing to run"? Any error messages? – ForceBru Mar 02 '20 at 19:45
  • 3
    Make sure you understand the [difference between single and double quotes](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash). – KamilCuk Mar 02 '20 at 19:47
  • With double quotes I am getting the following error message **warning: commands will be executed using /bin/sh Cannot open input file bash /home/corey/emailSignature/deleteUser.sh name: No such file or directory** however that command runs fine without running it in at now – MrToast Mar 02 '20 at 20:11
  • Aside: Using `.sh` extensions on executables (vs shell *libraries* meant to be sourced) is not great form. For code intended to be run with `bash` instead of `sh`, doubly so. Generally, UNIX commands don't have extensions -- you run `ls`, not `ls.elf`; this is similar to how when you have a Python *module* it has a `.py` extension, but when Python's setuptools creates an executable wrapper for a `main()` function or other entrypoint in that library, there's no extension at all. See also http://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/ – Charles Duffy Mar 02 '20 at 20:48

1 Answers1

2

-f expects a file to read commands from, but instead of a filename are giving it a command.

Feed the command on stdin instead (and remember to escape your parameter):

echo "bash /home/corey/emailSignature/deleteUser.sh ${1@Q}" | at now + ${3:-365} days
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • 1
    Note that with a shell too old to have `${var@Q}`, you'd want to `printf 'bash ... %q' "$1" | at now ...` – Charles Duffy Mar 02 '20 at 20:45
  • Will this command actually run the bash script or just echo what is within the quotes? – MrToast Mar 02 '20 at 20:57
  • @MrToast Both. When executed, it'll echo the output and give it to `at` as the command to run. Later, `at` will execute the command, which will run the bash script – that other guy Mar 02 '20 at 21:17