- What command means «do nothing» in a conditional in Bash?
- 3 Answers 3
- How to do nothing forever in an elegant way?
- 9 Answers 9
- Is there a Linux command that does nothing, but never exits? [duplicate]
- 6 Answers 6
- Not the answer you’re looking for? Browse other questions tagged command-line or ask your own question.
- Linked
- Related
- Hot Network Questions
- Is there a standard dummy executable file that does nothing in Linux?
- 6 Answers 6
- 20 Funny Commands of Linux or Linux is Fun in Terminal
- 1. Command: sl (Steam Locomotive)
- Install sl
- 2. Command: telnet
- 3. Command: fortune
- Install fortune
- 4. Command: rev (Reverse)
- 5. Command: factor
- 6. Command: script
- 7. Command: Cowsay
- Install Cowsay
- 8. Command: yes
- 9. Command: toilet
- Install toilet
- 10. Command: cmatrix
- Install cmatrix
- 11. Command: oneko
- Install oneko
- 12. Fork Bomb
- 13. Command: while
- 14. Command: espeak
- Install espeak
- 15. Command: aafire
- Install aafire
- 16. Command: bb
- 17. Command: curl
- 18. ASCIIquarium
- 19. Command: funny manpages
- 20. Linux Tweaks
- If You Appreciate What We Do Here On TecMint, You Should Consider:
What command means «do nothing» in a conditional in Bash?
Sometimes when making conditionals, I need the code to do nothing, e.g., here, I want Bash to do nothing when $a is greater than «10», print «1» if $a is less than «5», otherwise, print «2»:
This makes an error though. Is there a command which will do nothing and also not slow down my script?
3 Answers 3
The no-op command in shell is : (colon).
: (a colon)
Do nothing beyond expanding arguments and performing redirections. The return status is zero.
You can probably just use the true command:
An alternative, in your example case (but not necessarily everywhere) is to re-order your if/else:
Although I’m not answering the original question concering the no-op command, many (if not most) problems when one may think «in this branch I have to do nothing» can be bypassed by simply restructuring the logic so that this branch won’t occur.
I try to give a general rule by using the OPs example
do nothing when $a is greater than «10», print «1» if $a is less than «5», otherwise, print «2»
we have to avoid a branch where $a gets more than 10, so $a as a general condition can be applied to every other, following condition.
In general terms, when you say do nothing when X, then rephrase it as avoid a branch where X. Usually you can make the avoidance happen by simply negating X and applying it to all other conditions.
So the OPs example with the rule applied may be restructured as:
Just a variation of the above, enclosing everything in the $a condition:
(For this specific example @Flimzys restructuring is certainly better, but I wanted to give a general rule for all the people searching how to do nothing.)
Источник
How to do nothing forever in an elegant way?
I have a program which produces useful information on stdout but also reads from stdin . I want to redirect its standard output to a file without providing anything on standard input. So far, so good: I can do:
and don’t do anything in the tty.
However, the problem is I want to do this in the background. If I do:
the program will get suspended («suspended (tty input)»).
the program terminates immediately because it reaches EOF.
It seems that what I need is to pipe into program something which does not do anything for an indefinite amount of time and does not read stdin . The following approaches work:
However, this is all very ugly. There has to be an elegant way, using standard Unix utilities, to «do nothing, indefinitely» (to paraphrase man true ). How could I achieve this? (My main criteria for elegance here: no temporary files; no busy-waiting or periodic wakeups; no exotic utilities; as short as possible.)
9 Answers 9
I don’t think you’re going to get any more elegant than the
that you already suggested (assuming this uses inotify internally, there should be no polling or wakeups, so other than being odd looking, it should be sufficient).
You need a utility that will run indefinitely, will keep its stdout open, but won’t actually write anything to stdout, and won’t exit when its stdin is closed. Something like yes actually writes to stdout. cat will exit when its stdin is closed (or whatever you re-direct into it is done). I think sleep 1000000000d might work, but the tail is clearly better. My Debian box has a tailf that shortens command slightly.
Taking a different tack, how about running the program under screen ?
sleep infinity is the clearest solution I know of.
You can use infinity because sleep accepts a floating point number*, which may be decimal, hexadecimal, infinity, or NaN, according to man strtod .
* This isn’t part of the POSIX standard, so isn’t as portable as tail -f /dev/null . However, it is supported in GNU coreutils (Linux) and BSD (used on Mac) (apparently not supported on newer versions of Mac — see comments).
Yes, 2^31-1 is a finite number, and it won’t run forever, but I’ll give you $1000 when the sleep finally times out. (Hint: one of us will be dead by then.)
- no temporary files; check.
- no busy-waiting or periodic wakeups; check
- no exotic utilities; check.
- as short as possible. Okay, it could be shorter.
In shells that support them (ksh, zsh, bash4), you can start program as a co-process.
- ksh : program > output |&
- zsh , bash : coproc program > output
That starts program in background with its input redirected from a pipe . The other end of the pipe is open to the shell.
Three benefits of that approach
- no extra process
- you can exit the script when program dies (use wait to wait for it)
- program will terminate (get eof on its stdin if the shell exits).
You can create a binary that does just that with:
Here’s another suggestion using standard Unix utilities, to «do nothing, indefinitely».
This fires up a shell that is immediately sent SIGSTOP , which suspends the process. This is used as «input» to your program. The complement of SIGSTOP is SIGCONT , i.e. if you know the shell has PID 12345 you can kill -CONT 12345 to make it continue.
On Linux, you can do:
On Linux, opening /dev/fd/x where x is a file descriptor to the writing end of a pipe, gets you the reading end of the pipe, so here the same as on the stdin of program. So basically, read will never return, because the only thing that may write to that pipe is itself, and read doesn’t output anything.
It will also work on FreeBSD or Solaris, but for another reason. There, opening /dev/fd/1 gets you the same resource as open on fd 1 as you’d expect and as most systems except Linux do, so the writing end of the pipe. However, on FreeBSD and Solaris, pipes are bidirectional. So as long as program doesn’t write to its stdin (no application does), read will get nothing to read from that direction of the pipe.
On systems where pipes are not bidirectional, read will probably fail with an error when attempting to read from a write-only file descriptor. Also note that not all systems have /dev/fd/x .
Источник
Is there a Linux command that does nothing, but never exits? [duplicate]
I’m looking for a Linux command that does literally nothing, doesn’t output anything, but stays alive until ^C .
while true; do; done is not a good solution, because it is CPU intensive.
6 Answers 6
If we look at system calls, there’s actually one that does exactly that, pause (2):
pause() causes the calling process (or thread) to sleep until a signal is delivered .
Of course, then we’d just need a program that uses it. Short of compiling the two-liner C program below, the easiest way is probably with Perl:
Just add a sleep command.
will sleep for 10 minutes between loops.
GNU sleep and the sleep builtin of ksh93 (but not mksh ) accept any floating point number, not just integers, so you can do this:
Since you’ve mentioned ctrl-C I assume that you want to use it in interactive terminal. So you may just wait for input.
or just use arbitrary other commands which read from stdin like cat . They do «nothing» as long as there is no input.
or even better without using stdin:
This will not use stdin (as read would) and will sit waiting for new addition to /an/existing/regular/file (doesn’t work on some files: tail -f /dev/null will exit immediately. But will work for all regular files. If that file is not growing, the command will eat little cpu)
Not for forever, but there is sleep . You could combine your while loop with sleep — doesn’t even seem to tickle the cpus in my gkrellm monitor.
dr01 types faster than I do 🙂 . so more info — your cpu spiking is because it has to continually process the logic check with no pause between.
Or as a one-liner
while true; do sleep 100; done
Not the answer you’re looking for? Browse other questions tagged command-line or ask your own question.
Linked
Related
Hot Network Questions
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
Is there a standard dummy executable file that does nothing in Linux?
Is there a standard dummy executable file that does nothing in Linux? I have a shell command that always opens $EDITOR before the build process to input arguments manually. In my case, my arguments are always already set (this is an automated script) so I never need it, but it still pops up and awaits user input.
To solve this, I created an empty executable file that does nothing, so I can set EDITOR=dummy and the build script calls it, it exits and the build process can start.
My question is, is there an existing official file in Linux that when executed does nothing, a sort of placeholder that I could use for this purpose?
6 Answers 6
There’s the standard utilities true and false . The first does nothing but return an exit status of 0 for successful execution, the second does nothing but return a non-zero value indicating a non-successful result (*) . You probably want the first one.
Though some systems that really want you to enter some text (commit messages, etc.) will check if the «edited» file was actually modified, and just running true wouldn’t fly in that case. Instead, touch might work; it updates the timestamps of any files it gets as arguments.
However, if the editor gets any other arguments than the filename touch would create those as files. Many editors support an argument like +NNN to tell the initial line to put the cursor in, and so the editor may be called as $EDITOR +123 filename.txt . (E.g. less does this, git doesn’t seem to.)
Note that you’ll want to use true , not e.g. /bin/true . First, if there’s a shell involved, specifying the command without a path will allow the shell to use a builtin implementation, and if a shell is not used, the binary file will be found in PATH anyway. Second, not all systems have /bin/true ; e.g. on macOS, it’s /usr/bin/true . (Thanks @jpaugh.)
(* or as the GNU man page puts it, false «[does] nothing, unsuccessfully». Thanks @8bittree.)
Источник
20 Funny Commands of Linux or Linux is Fun in Terminal
Linux is fun! Huh. OK, so you don’t believe me. Mind me at the end of this article you will have to believe that Linux is actually a fun box.
20 Linux Funny Commands
1. Command: sl (Steam Locomotive)
You might be aware of command ‘ls‘ the list command, which is used frequently to view the contents of a folder but because of miss-typing sometimes you would result in ‘sl‘, how about getting a little fun in the terminal and not “command not found“.
Install sl
Output
This command works even when you type ‘LS‘ and not ‘ls‘.
2. Command: telnet
No! No!! it is not as much complex as it seems. You would be familiar with telnet. Telnet is a text-oriented bidirectional network protocol over a network. Here is nothing to be installed. What you should have is a Linux box and a working Internet.
telnet command
3. Command: fortune
what about getting your random fortune, sometimes funny in terminal.
Install fortune
4. Command: rev (Reverse)
It reverses every string given to it, is not it funny.
5. Command: factor
Time for some Mathematics, this command output all the possible factors of a given number.
6. Command: script
OK fine this is not a command and a script but it is nice.
7. Command: Cowsay
An ASCII cow in the terminal will say whatever you want.
Install Cowsay
Output
How about pipelining ‘fortune command‘, described above with cowsay?
Note: ‘|‘ is called pipeline instruction and it is used where the output of one command needs to be the input of another command. In the above example, the output of the ‘fortune‘ command acts as an input of the ‘cowsay‘ command. This pipeline instruction is frequently used in scripting and programming.
xcowsay is a graphical program that response similar to cowsay but in a graphical manner, hence it is X of cowsay.
Output
cowthink is another command just run “cowthink Linux is sooo funny” and see the difference in the output of cowsay and cowthink.
Output
8. Command: yes
It is funny but useful as well, especially in scripts and for System Administrators where an automated predefined response can be passed to the terminal or generated.
Note: (Till you interrupt i.e ctrl+c).
9. Command: toilet
what? Are u kidding, huh no! Definitely not, but for sure this command name itself is too funny, and I don’t know from where this command gets its name.
Install toilet
Output
It even offers some kind of color and font style.
toilet command
Note: Figlet is another command that more or less provides such a kind of effect in the terminal.
10. Command: cmatrix
You might have seen the Hollywood movie ‘matrix‘ and would be fascinated with the power, Neo was provided with, to see anything and everything in the matrix or you might think of an animation that looks like Hacker‘s desktop.
Install cmatrix
Output
11. Command: oneko
OK so you believe that the mouse pointer of Linux is the same silly black/white pointer were no animation lies then I fear you could be wrong. “oneko” is a package that will attach a “Jerry” with your mouse pointer and moves along with you pointer.
Install oneko
Output
Note: Once you close the terminal from which oneko was run, jerry will disappear, nor will start at start-up. You can add the application to start up and continue enjoying.
12. Fork Bomb
This is a very nasty piece of code. Run this at your own risk. This actually is a fork bomb which exponentially multiplies itself till all the system resource is utilized and the system hangs.
To check the power of this command you should try it once, but all at your own risk, close and save all other programs and files before running a fork bomb.
13. Command: while
The below “while” command is a script that provides you with a colored date and file till you interrupt (ctrl + c). Just copy and paste the below code into the terminal.
Linux while command
Note: The above script when modified with the following command, will give similar output but with a little difference, check it in your terminal.
14. Command: espeak
Just Turn the Knob of your multimedia speaker to full before pasting this command in your terminal and let us know how you felt listening to the god’s voice.
Install espeak
Output
15. Command: aafire
How about a fire in your terminal. Just type “aafire” in the terminal, without quotes, and see the magic. Press any key to interrupt the program.
Install aafire
Output
16. Command: bb
First, install the command and then, type “bb” in the terminal and see what happens.
bb command
17. Command: curl
Won’t it be an awesome feeling for you if you can update your Twitter status from the command line in front of your friend and they seem impressed? OK just replace username, password, and your status message with your’s username, password, and “your status message“.
18. ASCIIquarium
How it will be to get an aquarium in the terminal.
Install ASCIIquarium
Now Download and Install ASCIIquarium.
And finally, run “asciiquarium” or “/usr/local/bin/asciiquarium” in the terminal without quotes and be a part of the magic that will be taking place in front of your eyes.
aquarium command
19. Command: funny manpages
First, install the funny manpages and then run man pages for the commands below.
Some of them may be 18+, run at your own risk, they all are too funny.
20. Linux Tweaks
It is time for you to have some one-liner tweaks.
Linux is sexy: who | grep -i blonde | date; cd
; unzip; touch; strip; finger; mount; gasp; yes; uptime; umount; sleep (If you know what I mean)
There are certain others but these don’t work on all the systems and hence not included in this article. Some of them are man dog, filter, banner, etc.
Have fun, you can say me thanks later 🙂 yup, your comment is highly appreciated which encourages us to write more. Tell us which command you liked the most. Stay tuned I will be back soon with another article worth reading.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник