0

I'm using an CLI Arch Linux and I want to run an Shell/Bash Script to show the status of my Battery with acpi directly on the String Prompt(PS1).

I create the following Shell Script to Show me the Battery Status:

# Permition Acess: chmod +x loop.sh
# run .sh: ./loop.sh

i=true
#COLOR:
ORANGE='\e[33m'
STOP='\e[0m'

while ($i = true)
do
 printf ${ORANGE}
 echo $(clear)
 echo $(acpi -b)
 sleep 1
printf ${STOP}
done

My Idea is to Connect the Script on PS1 to keep showing the Battery Status always update!

My Current PS1 is:

PS1='[${OR}USER: \u ${B}TIME: \t ${C}DIR: \W ${RED}$(__git_ps1 " (%s)")]\n[${LG}$(acpi -b)${R}]\n\$

I'm calling the acpi but he only update when I use some command

F4NT0
  • 458
  • 1
  • 5
  • 13
  • 1
    So...what are you asking? – jesse_b May 18 '18 at 17:16
  • Connect a String on PS1 to run battery Status – F4NT0 May 18 '18 at 17:30
  • 2
    The bash PS1 string can contain a command to run, use the syntax: `$(command)`. – meuh May 18 '18 at 18:54
  • 2
    A cleaner solution would be use something like `screen` or `tmux` which can show a status line which remains independent of the rest of the tty. Some terminal emulators may also propose an escape sequence to create a status line, or to uptdate the title of the window and so on. – meuh May 18 '18 at 19:03
  • 1. you need to show us your current prompt `echo "$PS1"`; 2. [quote your variables](https://unix.stackexchange.com/q/171346/4667) with quotes. – glenn jackman May 18 '18 at 23:29
  • added! there is a way to initiate a shell script inside PS1? – F4NT0 May 18 '18 at 23:47

1 Answers1

1

there is no portable way to do what you want, but a shell specific method will probably work.

The prompt variables (PS1, PS2, etc.) have two specific and distinct types of evaluation that is mostly portable: assignment expansion which is exactly like any other variable assignment expansion which will allow for subcommand expansion is not suitable for battery monitoring as this expansion only happens once, and prompt expansion which might not allow command expansion but is expanded at every prompt display.

Note that there neither method provides a possibility for continuous battery monitoring, the best case is battery status when the prompt was last displayed.

Now for the non portable methods which will probably do what you want. Bash has two methods for executing arbitrary commands at prompt time: PROMPT_COMMAND and shopt promptvars. PROMPT_COMMAND is easy and straightforward, just set it to the command to run before showing the prompt. the shopt promptvars is more complicated as the quoting is more complicated. The main disadvantage is that both methods are bash specific, other shells will differ.

hildred
  • 5,759
  • 3
  • 30
  • 43
  • Thank you @hildred, i'm going to study this new way to improve this, when i find a descent way I will post the code or the commands – F4NT0 May 20 '18 at 16:18