LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2023, 08:06 PM   #1
lqmsh
Member
 
Registered: Oct 2021
Posts: 32

Rep: Reputation: Disabled
check if the script is run from Terminal or dmenu/bemenu?


Hi everyone,
I'm sure that most linux users have their own scripts somewhere in $PATH, like ~/.local/bin dir. I have many too. Some of them are to be run inside terminal and others are to be run directly from dmenu/bemenu (in my case, bemenu because using wayland). Later, I notice that the scripts should be able to determine themself whether should continue or not if it was executed inside terminal or from bemenu. It is because some scripts consist of interactive commands like read -p "type input: " and need to be run inside terminal and some are not.
In that case, how can I make my scripts to determine whether should continue or not?
According to my googling, I tested the following two, but not work. Both of them return "inside terminal" either running inside terminal or from bemenu.
Code:
#!/bin/bash
tty -s
if [ "0" == "$?" ]; then
    echo "inside terminal"
else
    echo "no terminal"
fi
Code:
#!/bin/bash
if [ -t 0 ]; then
   echo "inside terminal"
else
   echo "no terminal"
fi

Last edited by lqmsh; 04-07-2023 at 08:10 PM.
 
Old 04-08-2023, 12:50 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,479
Blog Entries: 1

Rep: Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695Reputation: 1695
These scripts do work, it is easy to test:
Code:
$ ./lqmsh.sh
inside terminal
inside terminal

$ ./lqmsh.sh </dev/null
no terminal
no terminal
See also: https://www.linuxquestions.org/quest...ms-4175605430/

Last edited by NevemTeve; 04-09-2023 at 02:27 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] configuring dmenu to run console applications tujv77 Linux - Software 4 06-05-2022 04:09 AM
Hotkeys no longer working in terminal based programs (mplayer, cmus, rtorrent etc.) and dmenu tab completion not working burning Slackware 8 06-19-2021 05:49 PM
how to Run .sh file directly from dmenu-i3wm? rdeyvil Linux - Newbie 3 06-27-2017 04:07 PM
Dmenu Turned Black michijo Linux - Software 0 11-02-2012 11:42 AM
Terminator & Dmenu spoovy Linux - Software 4 08-19-2010 03:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration