[SOLVED] Having issues with brace expansion in the bourne shell using the 'at' command.

About writing shell scripts and making the most of your shell
Forum rules
Before you post please read how to get help
Post Reply
magnetor1000
Level 2
Level 2
Posts: 80
Joined: Fri Apr 01, 2022 7:53 pm

[SOLVED] Having issues with brace expansion in the bourne shell using the 'at' command.

Post by magnetor1000 »

I just installed the at command to schedule specific an upcoming non-repeating task, which I have ran several times to test with and without the at command (and also I deleted the directories and folders that it creates each time after it runs so I can fully test it from scratch).

Whenever I schedule any task with the at command, it always warns me:
warning: commands will be executed using /bin/sh
(That's how I know it is using bourne, not bash)

This is the script I want it to run:

Code: Select all

#! /bin/bash  

mkdir /home/jay/Desktop/at_dir
touch /home/jay/Desktop/at_dir/{001..100}.txt
I run it like so: at 12:36 pm -f /home/jay/Desktop/bash_course/scripts/at_script.sh

It SHOULD create a directory on my desktop (that works), but also should create 100 empty files. When I run that script within bash, there is no issue. It creates all 100 files within that newly created directory.

However, when I schedule it to run using the at command, it creates one file {001..100}.txt.

So, how do I get the bourne shell to recognize brace expansion?

Another annoying thing about the at command is that it forces me to lay out the entire pathname to the script, despite the fact that it is within one of my $PATH directories. I assume that this is also due to it using the bourne shell instead of bash?

Or alternatively (and preferably), how do I get the at command to use bash instead of bourne? What is the benefit or advantage to using that environment when bash can do so much more?
Last edited by magnetor1000 on Fri Sep 30, 2022 3:17 pm, edited 1 time in total.
User avatar
AndyMH
Level 20
Level 20
Posts: 10778
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Having issues with brace expansion in the bourne shell using the 'at' command.

Post by AndyMH »

What's wrong with a cron job?
Homebrew i5-8400+GTX1080 Cinnamon 19.0, 4 x Thinkpad T430 Cinnamon 20.1, 2 x i7-3632 , i5-3320, i5-3210, Thinkpad T60 19.0 Mate
User avatar
xenopeek
Level 25
Level 25
Posts: 27594
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Having issues with brace expansion in the bourne shell using the 'at' command.

Post by xenopeek »

The -f option makes at immediately read the contents of the file, interpreting it as POSIX shell commands, and keeps them ready for the time you specified. This is not what you want.

Instead you should do it something like this:
echo '/home/jay/Desktop/bash_course/scripts/at_script.sh' | at 12:36 pm

The difference is that puts /home/jay/Desktop/bash_course/scripts/at_script.sh as the command to run at the specified time, instead of the contents of the file, and that makes it run normal so the hashbang is respected and your script runs with bash instead of sh.
Image
magnetor1000
Level 2
Level 2
Posts: 80
Joined: Fri Apr 01, 2022 7:53 pm

Re: Having issues with brace expansion in the bourne shell using the 'at' command.

Post by magnetor1000 »

Thank you, that worked!

As a side question, why didn't this version work?: echo $(at_dir.sh) | at 3:15 pm
User avatar
xenopeek
Level 25
Level 25
Posts: 27594
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: [SOLVED] Having issues with brace expansion in the bourne shell using the 'at' command.

Post by xenopeek »

magnetor1000 wrote:
Fri Sep 30, 2022 3:17 pm
why didn't this version work?: echo $(at_dir.sh) | at 3:15 pm
That immediately runs the contents of at_dir.sh, echos what the script outputs and then pipes that into at. What does at_dir.sh do? It's the first time you mention it.
Image
magnetor1000
Level 2
Level 2
Posts: 80
Joined: Fri Apr 01, 2022 7:53 pm

Re: [SOLVED] Having issues with brace expansion in the bourne shell using the 'at' command.

Post by magnetor1000 »

xenopeek wrote:
Fri Sep 30, 2022 3:47 pm
magnetor1000 wrote:
Fri Sep 30, 2022 3:17 pm
why didn't this version work?: echo $(at_dir.sh) | at 3:15 pm
That immediately runs the contents of at_dir.sh, echos what the script outputs and then pipes that into at. What does at_dir.sh do? It's the first time you mention it.
I meant at_script.sh. I just got confused because part of at_script.sh is that it creates a directory called at_dir. So I put in at_dir.sh , but I really meant at_script.sh.
magnetor1000
Level 2
Level 2
Posts: 80
Joined: Fri Apr 01, 2022 7:53 pm

Re: [SOLVED] Having issues with brace expansion in the bourne shell using the 'at' command.

Post by magnetor1000 »

So what I really meant was:

As a side question, why didn't this version work?: echo $(at_script.sh) | at 3:15 pm

What that version does is it runs the echo command immediately. So it immediately runs that script and at does nothing effectively here.
User avatar
xenopeek
Level 25
Level 25
Posts: 27594
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: [SOLVED] Having issues with brace expansion in the bourne shell using the 'at' command.

Post by xenopeek »

Yes, whatever at_script.sh outputs (what it prints to stdout) is sent to at. at interprets each line of output as a POSIX command. As you put it above, at_script.sh doesn't output anything it just runs the commands immediately that you wanted to run later.
Image
Post Reply

Return to “Scripts & Bash”