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

6
  • 1
    So...what are you asking?
    – jesse_b
    Commented May 18, 2018 at 17:16
  • Connect a String on PS1 to run battery Status
    – F4NT0
    Commented May 18, 2018 at 17:30
  • 2
    The bash PS1 string can contain a command to run, use the syntax: $(command).
    – meuh
    Commented May 18, 2018 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
    Commented May 18, 2018 at 19:03
  • 1. you need to show us your current prompt echo "$PS1"; 2. quote your variables with quotes. Commented May 18, 2018 at 23:29

1 Answer 1

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.

1
  • 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
    Commented May 20, 2018 at 16:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .