How to view process name in terminal-emulator tab or title bar [closed] - gnome-terminal

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How can I put the current running process name into a GNOME Terminal tab title (or title bar when there's only one tab)?
Although https://superuser.com/questions/42362/gnome-terminal-process-name-in-tab-title provides a solution (below), it completely clutters each tab with garbage when it starts so as to appear broken. Is there a better way?
case "$TERM" in
xterm*|rxvt*)
set -o functrace
trap 'echo -ne "\e]0;$BASH_COMMAND\007"' DEBUG
PS1="\e]0;\s\007$PS1"
;;
*)
;;
esac

Well, since everyone already seems to know David Pashley's solution I'm kind of surprised it took me so long to find this one.
It actually takes care of the bash-completion problem.
To be clear: I did nothing on my own here but research. All credit goes to Marius Gedminas (http://mg.pov.lt/blog/bash-prompt.html).
This works perfectly for me with Gnome-Terminal/Terminator
# If this is an xterm set the title to user#host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "\033]0;${USER}#${HOSTNAME}: ${PWD}\007"'
# Show the currently running command in the terminal title:
# http://www.davidpashley.com/articles/xterm-titles-with-bash.html
show_command_in_title_bar()
{
case "$BASH_COMMAND" in
*\033]0*)
# The command is trying to set the title bar as well;
# this is most likely the execution of $PROMPT_COMMAND.
# In any case nested escapes confuse the terminal, so don't
# output them.
;;
*)
echo -ne "\033]0;${USER}#${HOSTNAME}: ${BASH_COMMAND}\007"
;;
esac
}
trap show_command_in_title_bar DEBUG
;;
*)
;;
esac

Related

How to find out if running script/command as root/sudo using bash script [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I am following https://lifehacker.com/add-a-handy-separator-between-commands-in-your-terminal-5840450 to create a nice separator between commands in the terminal in Linux. Specifically, CentOS 8.
I am trying to modify the script to output the username of the user who ran the command.
Here is what I came up with.
# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill="--- "
reset_style='\[\033[00m\]'
if [ -z "$VIM" ];
then status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color
else status_style=$reset_style'\[\033[0;90;107m\]'
fi
prompt_style=$reset_style
command_style=$reset_style'\[\033[1;29m\]' # bold black
# Prompt variable:
OLD_PS1="$PS1"
PS1="$status_style"'$fill $USER \t\n'"$prompt_style$OLD_PS1$command_style"
# Reset color for command output
# (this one is invoked every time before a command is executed):
trap 'echo -ne "\e[0m"' DEBUG
function prompt_command {
# create a $fill of all screen width minus the time string and a space and USER and a space:
let fillsize=${COLUMNS}-10-${#USER}
fill=""
while [ "$fillsize" -gt "0" ]
do
fill="-${fill}" # fill with underscores to work on
let fillsize=${fillsize}-1
done
# If this is an xterm set the title to user#host:dir
case "$TERM" in
xterm*|rxvt*)
bname=`basename "${PWD/$HOME/~}"`
echo -ne "\033]0;${bname}: ${USER}#${HOSTNAME}: ${PWD/$HOME/~}\007"
;;
*)
;;
esac
}
PROMPT_COMMAND=prompt_command
Line 15 added " " and $USER to what is generated.
Line 25 changed to include an extra space and the length of the variable $USER
It looks just like I want it to.
Terminal Screenshot
But, I would like to update the code to output if I ran a command as sudo or not.
Ideally, it would change the name to root or whatever the root user name is.
I have tried several things, mainly I tried using whoami but this always returns my username not root.
If I run sudo whoami I get root but not from the script.
I also tried EUID No dice.
At this point, I have left the code in working condition with the $USER reference but I am willing to change it to whatever it needs to be.
Solution provided by u/pLumo
Solution Limitations:
There are cases not covered, for example sudo --user=some_user .... I think it's fairly easy to further enhance the awk script.
As it relies on the history, it won't work with commands you do not have in history, e.g. when using HISTCONTROL=ignoreboth and issue a command with a space in front.
# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill="--- "
reset_style='\[\033[00m\]'
if [ -z "$VIM" ];
then status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color
else status_style=$reset_style'\[\033[0;90;107m\]'
fi
prompt_style=$reset_style
command_style=$reset_style'\[\033[1;29m\]' # bold black
# Prompt variable:
OLD_PS1="$PS1"
PS1="$status_style"'$fill $name \t\n'"$prompt_style$OLD_PS1$command_style"
# Reset color for command output
# (this one is invoked every time before a command is executed):
trap 'echo -ne "\e[0m"' DEBUG
function prompt_command {
# create a $fill of all screen width minus the time string and a space and USER and a space:
name=$(fc -l -1 | awk -v u="$USER" '{if ($2=="sudo") { if ($3=="-u") u=$4; else u="root"; }; printf "%s",u}')
let fillsize=${COLUMNS}-10-${#name}
fill=""
while [ "$fillsize" -gt "0" ]
do
fill="-${fill}" # fill with underscores to work on
let fillsize=${fillsize}-1
done
# If this is an xterm set the title to user#host:dir
case "$TERM" in
xterm*|rxvt*)
bname=`basename "${PWD/$HOME/~}"`
echo -ne "\033]0;${bname}: ${USER}#${HOSTNAME}: ${PWD/$HOME/~}\007"
;;
*)
;;
esac
}
PROMPT_COMMAND=prompt_command
Terminal output:
Not sure if I should delete this question or not. Or if there is a better way to adjust this. Open to suggestions.

Getting error with if condition [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What is wrong with my below script?
I am new to scripting and trying out simple scripts. I am getting an error with my script.
START_OUT = `grep -c "Start Report" jeevagan/test_scripts/log.txt`
FINISH_OUT = `grep -c "Finished Report" jeevagan/test_scripts/log.txt`
if [$START_OUT == $FINISH_OUT]
then
echo "All good"
else
echo "Warning!!!Monitor Logs"
fi
bash/sh is very space sensitive. You want
if [ $START_OUT == $FINISH_OUT ]
Note the spacing around the brackets. "=" may be used instead of "==" for strict POSIX compliance. See here for more details and note the comment
In a script, the different parts of the if statement are usually
well-separated.
try leaving spaces on the if statement and add quotes on the variables, like this
if [ "$START_OUT" == "$FINISH_OUT" ]
If this is still not working try this exactly, this works for sure, if it still gives you an error, then use echo brefore the if statement and check what $START_OUT and $FINISH_OUT variables have stored, because the problem could be there, after you do this give us feedback:
if test "$START_OUT" = "$FINISH_OUT"
okayyyy, this was one of your problems, the other problem is that you left a space when you used grep and tried to add it in the variable...
i will rewrite your code, copy and paste it please. as you had it, it was like you calling the START_OUT command which obviously doesnt exist.... and tell me if it worked.
START_OUT=`grep -c "Start Report" jeevagan/test_scripts/log.txt`
FINISH_OUT=`grep -c "Finished Report" jeevagan/test_scripts/log.txt`
if test "$START_OUT" = "$FINISH_OUT"
then
echo "All good"
else
echo "Warning!!!Monitor Logs"
fi
if [$START_OUT == $FINISH_OUT] is wrong. take care about spaces. it should be like
if [ $START_OUT == $FINISH_OUT ]

Bash if/else error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a script to compile a program in bash and my if/else statement on line 84 is not working. I get this error:
./build2.sh: 84: [: no: unexpected operator
What is the problem?
http://www.pasteall.org/62904/bash
Thanks in advance!
"Is not working" is very faint. However, I'll give it a try...
If enter is pressed without typing any reply, $putProgramOn is empty, and
if [ $putProgramOn == "no" ] resolves to if [ == "no" ], which is a syntax error, so you need at least to quote the reply in the conditional:
if [ "$putProgramOn" == "no" ]
See also Bash Comparison Operators.
BTW: With esac you can simply have shortcuts "y" and "n" evaluated as well:
read -p 'Would you like to put the program on the flash drive? Awnser yes or no: ' putProgramOn
case "$putProgramOn" in
n|no)
echo "putProgramOn=no"
;;
y|yes)
echo "putProgramOn=yes"
;;
*)
echo "putProgramOn=\"putProgramOn\""
exit 1
;;
esac

Is it possible to access environment variables without the '$' in bash? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
This may seem pretty trivial but I am a novice in bash scripting and I could not find the answer elsewhere.
I have mplayer installed on ubuntu 13.04 and I am using it to stream internet radio stations. Normally you need to provide the ip as argument for that:
mplayer http://176.31.113.37:8080
I have created an aliases file in root .bash_aliases where I have the following:
export SOMA="http://173.239.76.147:8090"
export FRESH="http://176.31.113.37:8080"
Now I can play the radio using only the station name: mplayer $FRESH.
I was wondering is it possible to get rid of the $ sign also to use the command mplayer FRESH for example.
Thanks!
If you are using variables you need the $ to expand it. I would do this with aliases though, not variables. Consider something like this:
alias mpsoma='mplayer http://192.168.1.1:8090'
Then just type mpsoma to run the full command.
Sure, you can do this; you just need a function with built-in indirection. (In case you don't know, indirection is accessing a value through its name instead of through its value. In Bash, this is done by preceding the variable name with a ! in the parameter expansion.)
##
# Expand variables passed as arguments, otherwise just invoke mplayer normally.
#
mplayer() {
local first=${!1:-"$1"} # Expand to the indirection of $1 if it exists,
# otherwise expand to the value of "$1"
shift # Drop the 1st parameter. With enough focus, I could probably make this
# do the indirection on every positional parameter, but it's Friday.
command mplayer "$first" "$#"
}
If you put echo before the command mplayer part you can see what it's doing:
$ mplayer my name is dug
command mplayer my name is dug
$ my=your
$ mplayer my name is dug
command mplayer your name is dug
$ my=our mplayer my name is dug # You can even do in-place export assignment
command mplayer our name is dug
$ echo $my
your
I found your idea pretty nice so I thought about how this could be solved in another way. What came into my mind was to setup a function inside your ~/.bashrc. The function will be executable like any other command:
# Give the function a nice name - "mplayer" could be problematic
radio()
{
# Validate the number of parameters passed to our function
(( $# != 1 )) && {
printf "%s\n" "Please provide exactly one stream. Quitting."
return 1
}
local stream_alias stream_uri
# Assign the first parameter to a local variable
stream_alias="$1"
# Assign uris to a variable dependent on the value of the
# parameter passed to the function
case "$stream_alias" in
fresh)
stream_uri="http://176.31.113.37:8080"
;;
soma)
stream_uri="http://173.239.76.147:8090"
;;
*)
printf "%s\n" "Unknown stream. Quitting."
return 1
;;
esac
mplayer "$stream_uri" && {
printf "%s\n" "Playing radio station «${stream_alias}#${stream_uri#*\/\/}»."
}
return 0
}
This will give you the following results:
$ radio fresh
Playing radio station «fresh#176.31.113.37:8080».
$ radio soma
Playing radio station «soma#173.239.76.147:8090».
$ radio stream1
Unknown stream. Quitting.
Although there is room for improvement - like putting the uris and/or station names into (associative) arrays and maybe adding other features - I think you get the idea As an alternative, in cases where you don't want to put the function inside your .bashrc maybe because you want it to be available to all users, just use a little script and place it at a location which is in each users $PATH (of course you can add a path to it). I use /usr/local/bin for that purpose.
$ cat radio
#!/usr/bin/env bash
# Validate the number of parameters passed to our script
(( $# != 1 )) && {
printf "%s\n" "Please provide exactly one stream. Quitting."
exit 1
}
_mplayerStream()
{
local stream_alias stream_uri
# Assign the first parameter passed to the function
# to a local variable
stream_alias="$1"
# Assign uris to a variable dependent on the value of the
# parameter passed to the function
case "$stream_alias" in
fresh)
stream_uri="http://176.31.113.37:8080"
;;
soma)
stream_uri="http://173.239.76.147:8090"
;;
*)
printf "%s\n" "Unknown stream. Quitting."
exit 1
;;
esac
mplayer "$stream_uri" && {
printf "%s\n" "Playing radio station «${stream_alias}#${stream_uri#*\/\/}»."
}
return 0
}
_mplayerStream "$1"
$ radio soma
Playing radio station «soma#173.239.76.147:8090».
A really nice feature I could imagine would be tab completion showing a list of available streams defined inside the script.
You could make an alias like this:
alias FRESH="mplayer http://176.31.113.37:8080"
Then you can just type
FRESH
Put that line in the file .profile in your login directory and it will be available every time you log in.
Site note: I would use bash aliases in your case. You can write for example in your .bashrc (or better .bash_aliases file if it exists):
alias fresh='mplayer http://176.31.113.37:8080'
Now you can type fresh in your terminal to listen to this internet stream.

Can I change the input color of my Bash prompt to something different than the terminal default? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
My default terminal color is gray, and that's fine.
My Bash prompt displays a bunch of colors, and this works fine:
PS1="${COLOR_RED}\u${COLOR_WHITE}#${COLOR_RED}${COMPUTERNAME} ${COLOR_BLUE}\w${GITPROMPT} ${COLOR_RESET}"
But the text I type in, at the end of the prompt, is gray. I want it to be white (ANSI code "[37m").
If I add a COLOR_WHITE at the end of the prompt, instead of the COLOR_RESET, then the default terminal color changes to white until it is reset. This makes a weird effect of some gray text, with some white text bleeding through at the top.
How can I change the "input text" color of the Bash prompt, to something other than the terminal default color?
Simply add the following line:
export PS1=" \[\033[34m\]\u#\h \[\033[33m\]\w\[\033[31m\]\[\033[00m\] $ "
Preview:
These are my preferred colors. You can customize each part of the prompt's color by changing m codes (e.g., 34m) which are ANSI color codes.
List of ANSI color codes:
Black: 30m
Red: 31m
Green: 32m
Yellow: 33m
Blue: 34m
Purple: 35m
Cyan: 36m
White: 37m
Try this one. It is simpler:
export PS1="\e[0;32m\t \e[32;1m\u#\h:\e[0;36m\w\e[0m$ "
I would suggest changing your terminal emulator's settings.
It appears you are using iTerm2 (if you are on iTerm, I suggest looking at iTerm2), so:
Settings → Profiles → Your Profile → Color. Under 'basic colors', adjust 'foreground'.
For just changing the color of the input text, in Z shell (zsh) you could use a
preexec () { echo -ne "\e[0m" }
Source 1
I have found a hack-ish way to try this with Bash:
Not natively, but it can be hacked up using the DEBUG trap. This code sets up preexec and precmd functions similar to zsh. The command line is passed as a single argument to preexec.
Here is a simplified version of the code to set up a precmd function that is executed before running each command.
preexec () { :; }
preexec_invoke_exec () {
[ -n "$COMP_LINE" ] && return # do nothing if completing
local this_command=$(history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g");
preexec "$this_command"
}
trap 'preexec_invoke_exec' DEBUG
This trick is due to Glyph Lefkowitz; thanks to [bcat] for locating the original author.
http://www.macosxhints.com/dlfiles/preexec.bash.txt
Source 2
I found that in my installation of Debian 8 (Jessie) I already have this, but it is commented by default. It is
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u#\h:\w\$ '
fi
unset color_prompt force_color_prompt
I just uncommented the line that says force_color_prompt=yes.
If you didn't already have this in your .bashrc file then you can copy this and paste it in yours.

Resources