Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Mon 16 Sep 2019, 21:13
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How can a script check if it is run from a console? (Solved)
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 2 [19 Posts]   Goto page: 1, 2 Next
Author Message
scientist

Joined: 23 May 2015
Posts: 864

PostPosted: Thu 07 Apr 2016, 18:03    Post subject:  How can a script check if it is run from a console? (Solved)  

I have a few scripts that ONLY work if invoked from a console.

Is there a way for the script to check if it is run from a console ?

_________________
Thanks,
Andy


Slacko 6.3.0 FULL INSTALL
JWM
File Manager - Thunar
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 14305
Location: Gatineau (Qc), Canada

PostPosted: Thu 07 Apr 2016, 21:17    Post subject: Re: How can a script check if it is run from a console?  

scientist wrote:
I have a few scripts that ONLY work if invoked from a console.

Is there a way for the script to check if it is run from a console ?
Hi scientist.

Yeah. You check with the < ps > command if rxvt or urxvt is used to run the script.

Something like:
Code:
[ `"ps | awk '$4 ~ /rxvt/ && $0 ~ /name-of-the-script/'`" ] && name-of-the-script
Explanation: if "rxvt" is in position four (aka 4th field, for awk) of the output of
command < ps > AND the "name-of-the-script" is somewhere on that line, run it,
otherwise, do nothing.

You may have to put your present script in a function before the above line. Ex.:
Code:
#!/bin/sh
# blabla.sh
####
# Function
blabla (){
bla
ble
bli
blo
blu
}

[ `"ps | awk '$4 ~ /rxvt/ && $0 ~ /blabla/'`" ] && blabla
I've never done it exactly like you ask, so no guarantees. But I've done it with
symbolic links versus the actual script and it worked. (Launching a script from its
symbolic link rather than the script itself allows you to introduce one or multiple
variants.)

As if the script was saying: "If I exist in RAM, run me." Sounds silly said like that,
but it's a form of recursion, I think. It'll work if you fiddle with it a bit.

I hope this helps.

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1897
Location: Japan

PostPosted: Thu 07 Apr 2016, 21:35    Post subject:  

Code:
#!/bin/sh
if [ -t 1 ]; then
 echo "I'm running in a console"
else
 gxmessage "I'm not running in a console"
fi
Back to top
View user's profile Send private message 
scientist

Joined: 23 May 2015
Posts: 864

PostPosted: Thu 07 Apr 2016, 22:33    Post subject:  

MochiMoppel wrote:
Code:
#!/bin/sh
if [ -t 1 ]; then
 echo "I'm running in a console"
else
 gxmessage "I'm not running in a console"
fi


Thanks MochiMoppel.

Smile

_________________
Thanks,
Andy


Slacko 6.3.0 FULL INSTALL
JWM
File Manager - Thunar
Back to top
View user's profile Send private message 
scientist

Joined: 23 May 2015
Posts: 864

PostPosted: Thu 07 Apr 2016, 22:35    Post subject: Re: How can a script check if it is run from a console?  

musher0 wrote:
scientist wrote:
I have a few scripts that ONLY work if invoked from a console.

Is there a way for the script to check if it is run from a console ?
Hi scientist.

Yeah. You check with the < ps > command if rxvt or urxvt is used to run the script.

Something like:
Code:
[ `"ps | awk '$4 ~ /rxvt/ && $0 ~ /name-of-the-script/'`" ] && name-of-the-script
Explanation: if "rxvt" is in position four (aka 4th field, for awk) of the output of
command < ps > AND the "name-of-the-script" is somewhere on that line, run it,
otherwise, do nothing.

You may have to put your present script in a function before the above line. Ex.:
Code:
#!/bin/sh
# blabla.sh
####
# Function
blabla (){
bla
ble
bli
blo
blu
}

[ `"ps | awk '$4 ~ /rxvt/ && $0 ~ /blabla/'`" ] && blabla
I've never done it exactly like you ask, so no guarantees. But I've done it with
symbolic links versus the actual script and it worked. (Launching a script from its
symbolic link rather than the script itself allows you to introduce one or multiple
variants.)

As if the script was saying: "If I exist in RAM, run me." Sounds silly said like that,
but it's a form of recursion, I think. It'll work if you fiddle with it a bit.

I hope this helps.


I just found this that also works in addition to what Mochi posted.

Code:
#!/bin/bash
# DSL_Speed_Test.sh
# Determine DSL Upload and Download speeds and redirect to a text file.
# Copyright 2012-2014 Matt Martz
# All Rights Reserved.
# Uses speedtest_cli.py located at http://xmodulo.com/check-internet-speed-command-line-linux.html
#
# http://bytefreaks.net/gnulinux/bash/bash-script-how-to-check-if-it-run-by-a-user-in-terminal-or-not
tty -s;
if [ "0" == "$?" ]; then
echo "Speed test RUNNING FROM A CONSOLE";
else
gxmessage -timeout 2 "THIS MUST BE RUN FROM A CONSOLE";
exit 1
fi
cd /
cd root/Scripts
echo >> speed_test.txt
date "+DATE: %m/%d/%y%nTIME: %r" >> speed_test.txt
echo >> speed_test.txt
speedtest_cli.py >> speed_test.txt
echo "Speed test completed."
gxmessage -timeout 2 "Speed test complete."

_________________
Thanks,
Andy


Slacko 6.3.0 FULL INSTALL
JWM
File Manager - Thunar
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 14305
Location: Gatineau (Qc), Canada

PostPosted: Fri 08 Apr 2016, 08:27    Post subject:  

@MochiMoppei:

" -t 1 " ? Please explain.

You too, scientist, with your
Code:
tty -s;
 if [ "0" == "$?" ]; then
 echo "Speed test RUNNING FROM A CONSOLE";
Those are new to me.

Thanks in advance.

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
scientist

Joined: 23 May 2015
Posts: 864

PostPosted: Fri 08 Apr 2016, 09:14    Post subject:  

musher0 wrote:
@MochiMoppei:

" -t 1 " ? Please explain.

You too, scientist, with your
Code:
tty -s;
 if [ "0" == "$?" ]; then
 echo "Speed test RUNNING FROM A CONSOLE";
Those are new to me.

Thanks in advance.


I got that part of my script from here.

Code:
http://bytefreaks.net/gnulinux/bash/bash-script-how-to-check-if-it-run-by-a-user-in-terminal-or-not


More info on tty here.
I think it is rarely used.

Code:
http://www.linusakesson.net/programming/tty/

_________________
Thanks,
Andy


Slacko 6.3.0 FULL INSTALL
JWM
File Manager - Thunar
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 14305
Location: Gatineau (Qc), Canada

PostPosted: Fri 08 Apr 2016, 10:12    Post subject:  

Thanks, scientist. Very informative!
_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1897
Location: Japan

PostPosted: Fri 08 Apr 2016, 10:54    Post subject:  

musher0 wrote:
@MochiMoppei:
" -t 1 " ? Please explain.

[ -t 1 ] tests if the default file stdout (represented by its file descriptor 1) is opened in a terminal.
Simply speaking: The test evaluates to true if command output goes to a terminal screen.
[ -t 0 ] or [ -t 2 ] would also work (0=stdin, 2=stderr), so would a terse [ -t ] (in which case file descriptor 1 is assumed)
You can find a short explanation for the -t operator by typing help test into a terminal window.

BTW: Some Puppy scripts, notably the PPM stuff, try to sniff a console by checking if the $DISPLAY variable is set. Can detect only genuine text terminals and would look like this:
Code:
#!/bin/sh
if [ ! $DISPLAY ];then
echo "I'm running in a real text terminal. No X."
else
gxmessage "I'm running in X"
fi
A questionable approach, but fortunately easy to fool.
Back to top
View user's profile Send private message 
Karl Godt


Joined: 20 Jun 2010
Posts: 4208
Location: Kiel,Germany

PostPosted: Fri 08 Apr 2016, 16:26    Post subject:  

Code:
echo "what"

ps | grep -E '^bas|^ash|^sh'


should tell any enemy what to do ..

Am I now Judas ?

Oterherwise
Code:
echo $0
realpath $0

_________________
«Give me GUI or Death» -- I give you [[Xx]term[inal]] [[Cc]on[s][ole]] .
Macpup user since 2010 on full installations.
People who want problems with Puppy boot frugal Razz
Back to top
View user's profile Send private message 
scientist

Joined: 23 May 2015
Posts: 864

PostPosted: Fri 08 Apr 2016, 16:53    Post subject:  

Karl Godt wrote:
Code:
echo "what"

ps | grep -E '^bas|^ash|^sh'


[/code]


I don't understand what that code is doing ?

_________________
Thanks,
Andy


Slacko 6.3.0 FULL INSTALL
JWM
File Manager - Thunar
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 14305
Location: Gatineau (Qc), Canada

PostPosted: Fri 08 Apr 2016, 20:52    Post subject:  

scientist wrote:
Karl Godt wrote:
Code:
echo "what"

ps | grep -E '^bas|^ash|^sh'
I don't understand what that code is doing ?
Hi scientist.

Translation: The < ps > results are piped to egrep, who is looking for a "string that
begins with bas, ash or sh".

The "^" means "string begins with", in regex lingo. Except that it should be:
Code:
ps | grep -E 'bas$|ash$|sh$'
In regex lingo, "$" means "ends with". And, in good logic, that symbol is added
at the end of the string.

Reason: no bash script filename begins with sh. They all end with it, though
[or should].

Ref.:https://www.icewarp.com/support/online_help/203030104.htm

BFN.
echo_what_Godt_2016-04-08.jpg
 Description   Proof!
 Filesize   32.24 KB
 Viewed   190 Time(s)

echo_what_Godt_2016-04-08.jpg


_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1897
Location: Japan

PostPosted: Sat 09 Apr 2016, 02:10    Post subject:  

musher0 wrote:
Reason: no bash script filename begins with sh.
Says who? And reason for what? Try /usr/sbin/shutdownconfig
Quote:
They all end with it, though [or should]
Almost none of the Puppy scripts end with it.

Honestly, since you seem to understand Karl's post, what is his code supposed to do (it's easy to see what it does...) and how is it related to the thread topic?
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 14305
Location: Gatineau (Qc), Canada

PostPosted: Sat 09 Apr 2016, 11:09    Post subject:  

Karl's dog has eaten Karl's keyboard Smile? Where is Karl?
I hate answering in someone's place.

@MochiMoppei.

What is certain is that Karl won't find very many scripts with ^bas|^sh, etc. Those
are endings, so they should be bas$|sh$, etc. They should even be:
"\.bas$|\.sh$", etc.

Or use the < file > executable beforehand to determine the type of file. E.g.
Quote:
file Organise.sh
Organise.sh: POSIX shell script, UTF-8 Unicode text executable
But those two methods are beyond the subject, since they do not tell if the script
is run from a terminal.

This
Code:
ps | grep -E "\.bas$|\.sh$", etc.
only finds "running" scripts, -- with extension .bas or .sh, I know -- no matter
how they are launched.

BFN.

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
Karl Godt


Joined: 20 Jun 2010
Posts: 4208
Location: Kiel,Germany

PostPosted: Sun 10 Apr 2016, 00:29    Post subject:  

keyboard is there .
Just placed some other location .

Hmmm ..


ps actually is busybox ps .

/bin/ps is a script ,
that runs busybox ps if no arguments given .

Older xwin of Puppy had some interesting code :
Code:
#NUMXWINS=`pidof xwin | wc -w` #w478
#[ $NUMXWINS -gt 1 ] && exit   #w478

[ -z $DISPLAY ] || exit #v2.11 from GuestToo.
echo "This script will run X windows for you..."


This checks if it is already running .
But may have multiple pids .
And what if program is renamed like xwin-1.0.0 ?
Therefore realpath $0 .

ps -C programname
Code:
# ps -C xwin
  PID TTY          TIME CMD
 4753 tty1     00:00:00 xwin

This is the common Puppy way started by login running /etc/profile || / root/.profile .
It has still a controlling TTY .

This now started by rc.sysinit :
Code:
# ps -C udevd
  PID TTY          TIME CMD
  226 ?        00:00:00 udevd

Has no controlling terminal.

The TTY approach that had been given is best way I think .
Code:
_TTY_=`tty`
 [ "$_TTY_" = 'not a tty' ] && _TTY_=/dev/console



And for login shells it is
Code:
ps | grep -E -e '-bash|-ash|-sh'

_________________
«Give me GUI or Death» -- I give you [[Xx]term[inal]] [[Cc]on[s][ole]] .
Macpup user since 2010 on full installations.
People who want problems with Puppy boot frugal Razz
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 2 [19 Posts]   Goto page: 1, 2 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0615s ][ Queries: 12 (0.0059s) ][ GZIP on ]