rxvt: With and Without a Shell for a Script? [Solved]

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
ozboomer
Posts: 74
Joined: Sun Dec 20, 2020 12:49 am
Location: Blackburn, Australia
Has thanked: 7 times
Been thanked: 5 times

rxvt: With and Without a Shell for a Script? [Solved]

Post by ozboomer »

Hi again, all..

Straight to the point..

Considering the attached code... If I type rxvt & in an rxvt terminal, a new rxvt instance is opened, with rxvt (effectively) running its I/O on a bash shell process (it created?).

If I do something similar with rxvt -e myscript &, nothing happens... or, at least, a process is created and immediately exits (I'm guessing).

However, if I use a command like rxvt -e sh -c myscript &, rxvt runs.. and has an attached bash shell process in which to run the script, even though it's (mostly) the same as the first command I tried which had its own shell process created.

"Why is it so?" (thank you Professor Julius Sumner Miller)

Fanx!

Code: Select all

#!/bin/sh
# spawn - an example of how I'm dealing with running a script visibly
# in an rxvt window and logging the operation
#

my_proc="`basename $0`"
my_proc_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
my_proc_filespec="$my_proc_path/$my_proc"

logfile=/tmp/${my_proc}.log

if [[ -z "$1" ]]; then  # If nothing on the CLI...

   echo ""
   echo "$my_proc: CLI"
   echo "$my_proc: My PID : $$"
   echo "$my_proc: My PPID: $(ps -o ppid= -p $$)"

   echo "$my_proc: About to spawn..."

#   rxvt -title "$my_proc: Testing Shell rxvt" \
#        -sb -bg orange -geometry 80x25+200+200 \
#        -e "$my_proc_filespec TEST 2>&1 | tee ${logfile}" &

   rxvt -title "$my_proc: Testing Shell rxvt" \
        -sb -bg orange -geometry 80x25+200+200 \
        -e sh -c "$my_proc_filespec TEST 2>&1 | tee ${logfile}" &
        
   rxvt_pid=$!

   echo "$my_proc: rxvt PID: $rxvt_pid"
   echo "$my_proc: Log File: $logfile"

else                    # If any argument on the CLI...

   echo ""
   echo "$my_proc: DETACHED"
   echo "$my_proc: My PID : $$"
   echo "$my_proc: My PPID: $(ps -o ppid= -p $$)"
   echo ""
   
   echo -n "$my_proc: Countdown: "
   
   for ((i = 10; i >= 0; i--)); do
      echo -n "$i.. "
      sleep 1
   done
   echo ""
   echo ""
   
   read -p "$my_proc: Press ENTER to close this window... " kinput
   echo ""
      
fi

echo "$my_proc: Exiting..."
echo ""

# [eof]

Last edited by ozboomer on Sun Sep 17, 2023 7:47 am, edited 1 time in total.

Daily Use Puppies: F96-CE (migrating), Fossapup64 9.5, Xenial64 7.5, Slacko 6.3.2... Proud Puppy enthusiast since 2004
C, Perl, bash... for sysadmin, mini-apps... under DOS, Windoze, VMS, Linux... on PC, VAX... for 45+ years... :roll:

Burunduk
Posts: 245
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 123 times

Re: rxvt: With and Without a Shell for a Script?

Post by Burunduk »

What rxvt expects is:

rxvt -help wrote:

Usage: urxvt [options] [-e command args]

What you are trying to feed it with is:

rxvt ... -e "$my_proc_filespec TEST 2>&1 | tee ${logfile}" &

so basically

rxvt ... -e <shell code snippet with a pipe and other stuff>

rxvt is not a shell command interpreter - it cannot run this snippet and treats it as a file name. This is why you need sh -c.

This would work:

rxvt ... -e "$my_proc_filespec" TEST &

(Note that the argument is outside the quotes, -e "$my_proc_filespec TEST" would fail because there is no "myscript TEST" file.)

ozboomer
Posts: 74
Joined: Sun Dec 20, 2020 12:49 am
Location: Blackburn, Australia
Has thanked: 7 times
Been thanked: 5 times

Re: rxvt: With and Without a Shell for a Script?

Post by ozboomer »

Burunduk wrote: Sat Sep 16, 2023 11:16 am

so basically

rxvt ... -e <shell code snippet with a pipe and other stuff>

rxvt is not a shell command interpreter - it cannot run this snippet and treats it as a file name. This is why you need sh -c.

I've tried a few options in this vein and the only one that works 'properly' is my original verson, viz:

Code: Select all

   rxvt -title "$my_proc: Testing Shell rxvt" \
        -sb -bg orange -geometry 80x25+200+200 \
        -e sh -c "$my_proc_filespec TEST 2>&1 | tee ${logfile}" &  # ... Ok, non-empty log

#        -e "$my_proc_filespec TEST" > ${logfile} &                # ... no window, empty log
#        -e "$my_proc_filespec" TEST > ${logfile} &                # ... Ok, empty log
#        -e "$my_proc_filespec TEST       | tee ${logfile}" 2>&1 & # ... no window, no log
#        -e "$my_proc_filespec TEST       > ${logfile}     2>&1 &" # ... no window, no log
#        -e "$my_proc_filespec TEST"      | tee ${logfile} 2>&1 &  # ... no window, empty log
#        -e "$my_proc_filespec TEST" 2>&1 | tee ${logfile} &       # ... no window, empty log
#        -e "$my_proc_filespec" TEST 2>&1 | tee ${logfile} &       # ... Ok, empty log
#        -e sh -c "$my_proc_filespec TEST 2>&1 | tee ${logfile}" &  # ... Ok, non-empty log

Maybe there's another combination that I missed trying?

...or p'raps it's just as simple as running rxvt detached has no shell associated with it (unless you specify it with the sh -c) and therefore, can't run the script.

Daily Use Puppies: F96-CE (migrating), Fossapup64 9.5, Xenial64 7.5, Slacko 6.3.2... Proud Puppy enthusiast since 2004
C, Perl, bash... for sysadmin, mini-apps... under DOS, Windoze, VMS, Linux... on PC, VAX... for 45+ years... :roll:

Burunduk
Posts: 245
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 123 times

Re: rxvt: With and Without a Shell for a Script?

Post by Burunduk »

Maybe I haven't explained it clear enough. I'll try to repeat what I've said already:

Usage: urxvt [options] [-e command args] means that rxvt expects its -e option to be followed by a (simple) command and its optional arguments.
It doesn't expect a series of commands.

A command has to be a file name: absolute, relative, or a name of an executable that exists in the $PATH.
It can be, for example, a name of your script: /root/myscript, or ./myscript, or just myscript if you put it in one of the directories in the $PATH (for example, if it is /usr/local/bin/myscript).
Something like rxvt -e if [ -f myscript ]; then shfmt myscript; fi is not a simple command and is not going to work:
if is not found in the $PATH, the rxvt command ends at ;, then without if produces a syntax error.

Everything after the command name is treated as an argument to it.
But the -e option (unlike the shell comment) doesn't extend to the end of the line.

For example, in rxvt -hold -e echo I still believe this goes to >my.log,
the rxvt ... >my.log is a command
that has the option -e echo I still believe this goes to.
echo prints "I still believe this goes to" but rxvt itself prints nothing and the log file is empty.
If ">my.log" is quoted: rxvt -hold -e echo I still believe this goes to ">my.log",
it becomes an argument to echo and is printed by it too: "I still believe this goes to >my.log" (and no log is created).

rxvt can run and runs your script. But it cannot run a "command" /path/to/myscript TEST | tee /tmp/myscript.log because it's not your script, it's a shell script in its own rights and you need sh -c to interpret it.

ozboomer wrote: Sat Sep 16, 2023 11:54 pm

-e "$my_proc_filespec TEST" > ${logfile} & # ... no window, empty log

TEST is a part of the filename, a command "/path/to/myscript TEST" (with a space inside the filename) is not found hence no window, the rxvt command with all its arguments ends right before >. It outputs nothing itself - hence an empty log.

-e "$my_proc_filespec" TEST > ${logfile} & # ... Ok, empty log

TEST is an argument to /path/to/myscript, /path/to/myscript is found and runs, but as above, the rxvt command ends at > and outputs nothing to the log file.
The > ${logfile} & part is not related to the /path/to/myscript TEST, it redirects the output of the rxvt itself.

-e "$my_proc_filespec TEST | tee ${logfile}" 2>&1 & # ... no window, no log

There is no directory with a fancy long name "/path/to/myscript TEST | tee /tmp/", hence no window, no log. The 2>&1 & part has nothing to do with a command after the -e option - it's related to the rxvt command itself.

And so on...

To make the long story short, it's probably easier to just use what is working for you - -e sh -c ...

ozboomer
Posts: 74
Joined: Sun Dec 20, 2020 12:49 am
Location: Blackburn, Australia
Has thanked: 7 times
Been thanked: 5 times

Re: rxvt: With and Without a Shell for a Script?

Post by ozboomer »

Many thanks for the extended explanation(s), @burundunk; they help clarify the usage more than the stack of references I've been wading through.

...and I've bookmarked this thread as I'm sure I'll fall over this again :mrgreen:

Daily Use Puppies: F96-CE (migrating), Fossapup64 9.5, Xenial64 7.5, Slacko 6.3.2... Proud Puppy enthusiast since 2004
C, Perl, bash... for sysadmin, mini-apps... under DOS, Windoze, VMS, Linux... on PC, VAX... for 45+ years... :roll:

User avatar
fredx181
Posts: 2648
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 293 times
Been thanked: 1042 times
Contact:

Re: rxvt: With and Without a Shell for a Script? [Solved]

Post by fredx181 »

Can be handy also by using sh -c to run a function in rxvt:

Code: Select all

function run_in_rxvt () {
echo do_this
echo do_that
}
export -f run_in_rxvt

rxvt -hold -e sh -c run_in_rxvt
User avatar
mikewalsh
Moderator
Posts: 5666
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 605 times
Been thanked: 1740 times

Re: rxvt: With and Without a Shell for a Script?

Post by mikewalsh »

@Burunduk :-

Burunduk wrote: Sat Sep 16, 2023 11:16 am

rxvt is not a shell command interpreter - it cannot run this snippet and treats it as a file name. This is why you need sh -c.

Um.......I'm not entirely sure as that's true?

I do use rxvt display windows in a few of my utilities for displaying output as information from certain commands:-

MultiCam and CamRecord both use it for displaying available audio cards:-

Code: Select all

rxvt -title '       ~ List ALL audio devices.....' -background '#ffff80' -geometry '80x20' rxvt -hold ls -e arecord -l

.....and available video devices:-

Code: Select all

rxvt -title '       ~ List ALL video devices.....' -background '#ffff80' -geometry '80x12' rxvt -hold ls -e v4l2-ctl --list-devices

However, in the most recent build of CamRecord, I've since switched to using a gxmessage window for the output instead:-

Code: Select all

#!/bin/sh
#
arecord -l > /tmp/audio.txt
gxmessage -file /tmp/audio.txt
#
rm -f /tmp/audio.txt

....and

Code: Select all

#!/bin/sh
#
v4l2-ctl --list-devices > /tmp/video.txt
gxmessage -file /tmp/video.txt
#
rm -f /tmp/video.txt

.....which to my way of thinking is just easier.....

(*shrug...*)

Mike. Image

Puppy "stuff" ~ MORE Puppy "stuff" ~ ....and MORE! :D
_______________________________________________________

Image

Burunduk
Posts: 245
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 123 times

Re: rxvt: With and Without a Shell for a Script? [Solved]

Post by Burunduk »

mikewalsh wrote: Sun Sep 17, 2023 10:39 pm
Burunduk wrote: Sat Sep 16, 2023 11:16 am

rxvt is not a shell command interpreter - it cannot run this snippet and treats it as a file name. This is why you need sh -c.

Um.......I'm not entirely sure as that's true?

In turn, I'm not sure as to what is that that. The thread is about the difference between rxvt -e command and rxvt -e sh -c command.

Neither the current shell nor the rxvt terminal emulator itself actually interpret the -e option as a shell snippet.
The shell makes its expansions (e.g. it expands variables) and rxvt then tries to run the resulting command. The first word is treated as a command, the other ones - as arguments to the command.

Burunduk wrote: Sun Sep 17, 2023 3:03 am

A command has to be a file name: absolute, relative, or a name of an executable that exists in the $PATH.
It can be, for example, a name of your script: /root/myscript, or ./myscript, or just myscript if you put it in one of the directories in the $PATH (for example, if it is /usr/local/bin/myscript).
Something like rxvt -e if [ -f myscript ]; then shfmt myscript; fi is not a simple command and is not going to work:
if is not found in the $PATH, the rxvt command ends at ;, then without if produces a syntax error.

If you think this is not true, you can try to run the above example. Or this one:

rxvt -hold -e ulimit

ulimit is a shell builtin and is not found in the $PATH. rxvt reports an error.

For this to work you do need sh -c:

urxvt -hold -e sh -c ulimit

(echo or pwd will work because they are also /bin/echo and /bin/pwd, at least on my Fossapup.)

mikewalsh wrote: Sun Sep 17, 2023 10:39 pm

Code: Select all

rxvt -title '       ~ List ALL audio devices.....' -background '#ffff80' -geometry '80x20' rxvt -hold ls -e arecord -l

This works without sh -c because arecord -l is a simple command, there is nothing to interpret here.
(The second rxvt is superfluous though.)

That's the point.

However, in the most recent build of CamRecord, I've since switched to using a gxmessage window for the output instead:-

The gxmessage seems to be a different story here - of course, rxvt is not the only way to show a message.

By the way, gxmessage can read the stdin. So it's not necessary to use a tempfile:

Code: Select all

arecord -l | gxmessage -title "List capture devices..." -file -

---

fredx181 wrote: Sun Sep 17, 2023 1:20 pm

Can be handy also by using sh -c to run a function in rxvt:

The exported function is also available in scripts started without sh -c:

Code: Select all

do_that () { echo do_that; }
export -f do_that
echo '#!/bin/sh
do_that' > do_that
chmod 755 do_that
rxvt -hold -e ./do_that

not handy at all this way though.

Post Reply

Return to “Programming”