Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect empty command

Tags:

Consider this PS1

PS1='\n${_:+$? }$ ' 

Here is the result of a few commands

$ [ 2 = 2 ]  0 $ [ 2 = 3 ]  1 $  1 $ 

The first line shows no status as expected, and the next two lines show the correct exit code. However on line 3 only Enter was pressed, so I would like the status to go away, like line 1. How can I do this?

like image 965
Zombo Avatar asked Dec 09 '14 17:12

Zombo


People also ask

How do I check if a file is empty?

You can use the find command and other options as follows. The -s option to the test builtin check to see if FILE exists and has a size greater than zero. It returns true and false values to indicate that file is empty or has some data.

What is empty command?

The empty statement is a semicolon ( ; ) indicating that no statement will be executed, even if JavaScript syntax requires one. The opposite behavior, where you want multiple statements, but JavaScript only allows a single one, is possible using a block statement, which combines several statements into a single one.

How do you check if a parameter is empty?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.

How do you check if a $1 is empty in bash?

To find out if a bash variable is empty: Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty" Determine if a bash variable is empty: [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"


1 Answers

Here's a funny, very simple possibility: it uses the \# escape sequence of PS1 together with parameter expansions (and the way Bash expands its prompt).

The escape sequence \# expands to the command number of the command to be executed. This is incremented each time a command has actually been executed. Try it:

$ PS1='\# $ ' 2 $ echo hello hello 3 $ # this is a comment 3 $ 3 $    echo hello hello 4 $ 

Now, each time a prompt is to be displayed, Bash first expands the escape sequences found in PS1, then (provided the shell option promptvars is set, which is the default), this string is expanded via parameter expansion, command substitution, arithmetic expansion, and quote removal.

The trick is then to have an array that will have the k-th field set (to the empty string) whenever the (k-1)-th command is executed. Then, using appropriate parameter expansions, we'll be able to detect when these fields are set and to display the return code of the previous command if the field isn't set. If you want to call this array __cmdnbary, just do:

PS1='\n${__cmdnbary[\#]-$? }${__cmdnbary[\#]=}\$ ' 

Look:

$ PS1='\n${__cmdnbary[\#]-$? }${__cmdnbary[\#]=}\$ '  0 $ [ 2 = 3 ]  1 $   $ # it seems that it works  $     echo "it works" it works  0 $ 

To qualify for the shortest answer challenge:

PS1='\n${a[\#]-$? }${a[\#]=}$ ' 

that's 31 characters.

Don't use this, of course, as a is a too trivial name; also, \$ might be better than $.


Seems you don't like that the initial prompt is 0 $; you can very easily modify this by initializing the array __cmdnbary appropriately: you'll put this somewhere in your configuration file:

__cmdnbary=( '' '' ) # Initialize the field 1! PS1='\n${__cmdnbary[\#]-$? }${__cmdnbary[\#]=}\$ ' 
like image 117
gniourf_gniourf Avatar answered Oct 07 '22 05:10

gniourf_gniourf