I'm calling the sleep command from awk like so: system("sleep 15m") and found that when I interrupt that with CTRL-C it only interrupts the sleep command, not the script. So I tried wrapping the system function in an if statement, as suggested  and further explained . It then looks like if (system("sleep 15m") != 0) exit 1.  That didn't work, so I tried print system("sleep 3"), which always returns 0, contrary to the stated behavior. Typing sleep 3 then echo $? in the shell produces 0 if I let it run and 130 if I interrupt it.  So what am I missing? I've checked for typos several times, tried the --posix and --traditional switches, and studied the user's guide, nothing changes it.  $ awk --version GNU Awk 4.1.3, API: 1.1 (GNU MPFR 3.1.4, GNU MP 6.1.0) Copyright (C) 1989, 1991-2015 Free Software Foundation.
121723
I'm having an issue with the following piece of (G)AWK script:  do {     ... } while (system("sleep 10"))   My intention is to break the loop when the user presses ^C during the sleep, but it's not working.  I believe the problem is that Bash exits with 0 when interrupted with ^C, at least when it's executed by AWK's system():  $ awk 'BEGIN { print "\n" system("sleep 2") }' (let the sleep complete) 0      $ awk 'BEGIN { print "\n" system("sleep 2") }' ^C 0   Why is it so?   Is this a bug in Bash or in (G)AWK?  Is there a simple solution that doesn't involve complicated Bash-specific syntax like trap?  The best I could come up with is this:  do {     ... } while (42 == system("sleep 10 &amp;&amp; exit 42"))   which still looks like a kludge to me.
21443
Is there any standard that covers the portability of running a command after variable assignment on the same line?  APPLE="cider" echo hi   How portable is something like that? Where will it work and where won't it?   Also: my shell scripts start with #!/bin/sh if that makes any difference.
91218
I have a command that I want to have run again automatically each time it terminates, so I ran something like this:  while [ 1 ]; do COMMAND; done;   but if I can't stop the loop with Ctrl-c as that just kills COMMAND and not the entire loop.  How would I achieve something similar but which I can stop without having to close the terminal?
108168
In this line:  Reply from 10.11.12.13 time=1035ms   how to write an awk script which brings out just 1035.
38906
I want to start process (eg. myCommand) and get its pid (to allow to kill it later).   I tried ps and filter by name, but I can not distinguish process by names  myCommand ps ux | awk '/&lt;myCommand&gt;/ {print $2}'    Because processes names are not unique.  I can run process by:  myCommand &amp;   I found that I can get this PID by:   echo $!   Is there any simpler solution?  I would be happy to execute myCommand and get its PID as a result of one line command.
68468
In bash, the key that normally outputs the i character, stays dead. It does not output anything, no space no character, multiple keystrokes of same key do neither.  This is happening on trisquel8, out of the blue, no upgrades, and reproducible after startups, restarts, halt and hard restart. It's only happening within the xterm, the mate-terminal, the console... at the bash prompt (shell used is bash). Any other application WILL recognize the i as always. The login prompt recognizes i.  Hardware is tested and functional. Asus SyncMaster laptop, internal and external Logitec keyboard produce the same ¨silence¨(no output).  Suppose it must be something to do with bash specific issues. Within commands (nano, since vim has an i which cannot be typed to call vim), there is also the regular output expected i.  Again stumped, this incapacitates the whole system, and that with no probable preliminary settings tweaks.
32103
I want to generate a sorted list with all 8-digit numbers — from 00000000 to 99999999. I typed in the shell:  f() {  while IFS="" read -r line; do    for i in {0..9}; do         echo "$line$i";    done;  done }  echo | f | f | f | f | f | f | f | f | tee result.txt | wc -l   response is     bash: echo: write error: Interrupted system call bash: echo: write error: Interrupted system call bash: echo: write error: Interrupted system call 99998890   Why have I got these three errors and malformed result.txt ?  I use  GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)  Debian GNU/Linux 9.6 (stretch)  Linux kernel: 4.19.0 #2 SMP Thu Nov 1 15:31:34 EET 2018 x86_64 GNU/Linux
34425
Situation: I have a script watching over NumLock status, well not really watching, but turning it On every 1 second. The script is running in the background.  Reasoning: I often accidentally turn off NumLock. And I have no indicator of NumLock status on the keyboard.  OS, DE, DM, WM, xmodmap:  :  $ lsb_release -a  No LSB modules are available. Distributor ID: LinuxMint Description:    Linux Mint 18 Sarah Release:    18 Codename:   sarah   :  echo $DESKTOP_SESSION  cinnamon   :  cat /etc/X11/default-display-manager  /usr/sbin/mdm   :  wmctrl -m | head -n 1  Name: Mutter (Muffin)   :  xmodmap -pm  xmodmap:  up to 4 keys per modifier, (keycodes in parentheses):  shift       Shift_L (0x32),  Shift_R (0x3e) lock        Caps_Lock (0x42) control     Control_L (0x25),  Control_R (0x69) mod1        Alt_L (0x40),  Alt_R (0x6c),  Meta_L (0xcd) mod2        Num_Lock (0x4d) mod3       mod4        Super_L (0x85),  Super_R (0x86),  Super_L (0xce),  Hyper_L (0xcf) mod5        ISO_Level3_Shift (0x5c),  Mode_switch (0xcb)   My original Bash script follows:  #!/bin/bash  while true do    numlockx on    sleep 1s  done   As you can see, the script does not care about current status of NumLock. It just keeps turning it on.  Goal: I would like to make the script at least somewhat CPU efficient.  Question: What is the most CPU efficient way to ensure that NumLock is On in Linux (Mint 18)?
79332
I seem to misunderstand the Bash rule for creating a subshell. I thought parentheses always creates a subshell, which runs as its own process.  However, this doesn't seem to be the case. In Code Snippet A (below), the second sleep command does not run in a separate shell (as determined by pstree in another terminal). However, in Code Snippet B, the second sleep command does run in a separate shell. The only difference between the snippets is that the second snippet has two commands within the parentheses.  Could somebody please explain the rule for when subshells are created?  CODE SNIPPET A:  sleep 5 ( sleep 5 )   CODE SNIPPET B:  sleep 5 ( x=1 sleep 5 )
54428
After running the command     { sleep 5; } &amp;     output of ps is   (output 1)   PID TTY           TIME CMD   972 ttys000    0:00.27 -bash  2556 ttys000    0:00.00 -bash  2557 ttys000    0:00.00 sleep 5      while for    ( sleep 5 ) &amp;       out of ps is  (output 2)   PID TTY           TIME CMD  972 ttys000    0:00.28 -bash 2566 ttys000    0:00.00 sleep 5     () causes a subshell environment and I expect "output 1" for this case as it results in forking a child process while I expects "output 2" for { sleep 5; } &amp; as it executes in the current shell. This might look a silly question but I really do not understand this behavior. What I am missing here?
112122
Can anybody explain to me what the shell does in the two examples A) and B) below? It obviously behaves differently, but I can't find out why the output is different.  Example: Let's have a script in our current directory named bla.sh with only one command: echo ${0##/*} hello  A) Started as: ./bla.sh gives: ./bla.sh hello  B) Started as: . bla.sh gives: -bash hello  Since I use this in a script, the second output (because of the "-" in front of the -bash) kills the command. Of course, a simple -- before the ${...} helped, but I would love to understand what causes the output in the first place.I love bash. And vi[m]. But I digress…
47297
I have a program that receives an argument and prints it every second:    ~# ./myapp 234 234 234 234 234 ...     I want to be able to run it in the background and right after a shell but it must be in one line (I don't want to explain why I need it because I don't want to make the discussion too complicated right now).    This is what I tried and the error I received:    ~# bash -c "./myapp 234 &amp;; sh"  bash: -c: line 0: syntax error near unexpected token `;' bash: -c: line 0: `./myapp 234 &amp;; sh'   I also tried with &amp;&amp; as separator:    ~# bash -c "./myapp 234 &amp; &amp;&amp; sh"  bash: -c: line 0: syntax error near unexpected token `&amp;&amp;' bash: -c: line 0: `./myapp 234 &amp; &amp;&amp; sh'   Any idea how I can solve it?    EDIT: Thanks to @roaima. I also found a link that talks about it:
91365
Is it possible to write commands to text file and then loaded it into terminal as file? If yes, how is the command for loading the file? Thank you.  For instance file_commands:  awk -f program.awk d01.active &gt; out1 awk -f program.awk d02.active &gt; out2   It is because of a problem with running an awk program that doesn't work with command   awk -f program.awk d??.active &gt; out   I need to use program.awk for lots of files and this seemed to me as easier solution when I am not able to repair program for that command with ??.  It is related with this question
110271
I am having trouble with bash. I am trying to put a command in an if statement and then compare it to a string.  This works perfectly.  echo $(ipcs | grep Shared | awk '{print $2}')   When I put it in an if statement I get some problems.  $ if [ "$(ipcs | grep Shared | awk '{print $2}')" -eq "Shared"]; then   echo expression evaluated as true; else   echo expression evaluated as false; fi bash: [: missing `]' expression evaluated as false  $ if [ "$(ipcs | grep Shared | awk '{print $2}')" = "Shared"]; then   echo expression evaluated as true; else   echo expression evaluated as false; fi bash: [: missing `]' expression evaluated as false  $ if [ "$(ipcs | grep Shared | awk '{print $2}')" == "Shared"]; then   echo expression evaluated as true; else   echo expression evaluated as false; fi bash: [: missing `]' expression evaluated as false   I tried ==, =, and -eq because I wasn't sure which one to use.
778
Usually, $0 in a script is set to the name of the script, or to whatever it was invoked as (including the path). However, if I use bash with the -c option, $0 is set to the first of the arguments passed after the command string:  bash -c 'echo $0' foo bar  # foo    In effect, it seems like positional parameters have been shifted, but including $0. However shift in the command string doesn't affect $0 (as normal):  bash -c 'echo $0; shift; echo $0' foo bar # foo # foo   Why this apparently odd behaviour for command strings? Note that I am looking for the reason, the rationale, behind implementing such odd behaviour.     One could speculate that such a command string wouldn't need the $0 parameter as usually defined, so for economy it is also used for normal arguments. However, in that case the behaviour of shift is odd. Another possibility is that $0 is used to define the behaviour of programs (a la bash called as sh or vim called as vi), but that cannot be, since $0 here is only seen in the command string and not by programs called within it. I cannot think of any other uses for $0, so I am at a loss to explain this.
16837
I've written a quick-and-dirty script to time some reports from a web service:  BASE_URL='http://example.com/json/webservice/' FIRST=1 FINAL=10000  for report_code in $(seq 1 $FINAL); do   (time -p response=$(curl --write-out %{http_code} --silent -O ${BASE_URL}/${report_code}) ) 2&gt; ${report_code}.time    echo $response  # &lt;------- this is out of scope!  How do I fix that?   if [[ $response = '404' ]]; then     echo "Deleting report # ${report_code}!"     rm ${report_code}   else     echo "${report_code} seems to be good!"   fi done   I need to wrap the time command in a subshell so I can redirect its output, but that makes the value of $response unavailable to the parent shell.  How do I get around this problem?
82839
Why is it that using bash and suspending a while loop, the loop stops after being resumed? Short example below.  $ while true; do echo .; sleep 1; done . . ^Z [1]+  Stopped                 sleep 1 $ fg sleep 1 $   I'm familiar with signals, and I'm guessing this may be the natural behaviour of bash here, but I'd like to better understand why it behaves in this particular way.