Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. Personally I'd ratter go with -z that's more realiable. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Checking for null string in bash Ask Question.
It doesn't say anything one way or the other as to whether any input is available. A simple solution is to use ifne command if input not empty. In some distributions, it is not installed by default. It is a part of the package moreutils in most distros. Note that if the standard input is not empty, it is passed through ifne to the given command.
I used the examples from the original question. If you don't want the piped data use -q option with grep. Old question, but in case someone comes across it as I did: My solution is to read with a timeout. If stdin is empty, this will return after 5 seconds.
Otherwise it will read all the input and you can process it as needed. I cannot think of any standard utility doing just that, so here is a trivial program doing it better than the ifne from moreutils IMHO ;-. If there's no data available on stdin, it will exit with status 1. If there's data, it will run prog. If no prog is given, it will exit with status 0.
Detects if an input has data without reading anything. Then you can read the input if the input is available at the time the read is executed :. Note: Understand that this depends on timing. This detects if input already has data only at the time read -t runs. The echo writes some data but is not very fast to start and write its first byte, thus, read -t 0 will report that its input is empty, since program has not written anything yet.
You want the opposite thus the not operator! Be sure to use echo -n "" in your testing because echo "" will output a newline. If you need this functionality for use in xargs and you have GNU xargs, you can use xargs -r. All you need to do afterwards is to check if contents is empty:.
This seems to be a reasonable ifne implementation in bash if you're ok with reading the whole first line. I added the -q to suppress the grep ped string output here, so you only see "yes" or "no". Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
The only change that we have made to this script is that we have made our string null by assigning it the null value, as you can see in the image shown below:. We will copy the script shown in the image below in our Bash file. If that will be the case, this script will prompt the user to enter a valid string, and then it will print that string. After running this Bash script, when we were prompted to enter a string, we intentionally entered a null string, i.
However, in all 3 cases the issue is the same - unset variable will be treated as empty, thus what the shell sees with these unquoted variables is. Remember what I said about improper order of command-line arguments? Hence why quoting is important. Let's enable diagnostic output and see what shell executes:. It also can be combined as in. Of course we could use -z flag, however according to research in some shells, particularly ksh88 according to Stephane Chazelas , this flag is faulty.
Ubuntu Community Ask! Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Ask Question. Asked 7 years, 9 months ago. Active 2 years, 6 months ago.
0コメント