Tag Info

Hot answers tagged

15

Circular I/O Loop Implemented with tail -f This implements a circular I/O loop: $ echo 1 >file $ tail -f file | while read n; do echo $((n+1)); sleep 1; done | tee -a file 2 3 4 5 6 7 [..snip...] This implements the circular input/output loop using the sine algorithm that you mentioned: $ echo 1 >file $ tail -f file | while read n; do echo ...


9

You can use a FIFO for this, created with mkfifo. Note however that its very easy to accidentally create a deadlock. Let me explain that—take your hypothetical "circular" example. You feed a command's output to its input. There are at least two ways this might deadlock: The command has an output buffer. It's partially filled, but hasn't been flushed ...


8

The function-based approach results in the clearest code. There's at least two variants possible, the one suggested by FloHimself above in the comment (which would not be my preferred way): function myprint { if type banner >/dev/null then banner "$@" else echo "$@" fi } myprint "Hello World!" or - the variant I'd prefer - to use a ...


4

You're looking for the chpasswd command. You'd do something like this: echo 'pi:newpassword' | chpasswd # change user pi password to newpassword Note that it needs to be run as root, at least with the default PAM configuration. But presumably run as root isn't a problem for a system deployment script. Also, you can do multiple users at once by feeding it ...


3

Inter process communication isn't a small question. There is a lot of "it depends" in there. Perl has a whole chapter on the subject under perlipc However for your scenario as described - I'd write a state file, flock it and then write your data there. The flock ensures you don't have a race condition, and the file can be of arbitrary length. You can use ...


3

You can have some kind of watchdog script running that requires a signal via the remote connection every n (milli)seconds and have it do something when not receiving the signal. See How to introduce timeout for shell scripting? for an example of an expect-script that prints a message on timeout; you can alter it to create a safe shutdown. The watchdog echo ...


3

This seems to work: #!/bin/sh pressed_ctrl_c= trap "pressed_ctrl_c=1" INT while true do (trap "" INT; foo)& wait || wait if [ "$pressed_ctrl_c" ] then # echo break fi done Initialize pressed_ctrl_c to null.  This is probably not necessary in a script. trap command signum tells ...


3

You could try this sort of construct: #!/bin/bash # INTR= trap 'INTR=yes; echo "** INTR **" >&2' INT while : do ( # Protect the subshell block trap '' INT # Protected code here echo -n "The date/time is: " sleep 2 date read -t2 -p 'Continue (y/n)? ' YN || echo test n = "$YN" ...


3

flag=0 testuser=$(stat "/home/testuser" -c %U) while IFS=':' read -r myuser a b c d mydir e do if [ -d "$mydir" -a "$e" != "/sbin/nologin" ] then if [ "$myuser" != "$testuser" -a \ "$myuser" != $(stat "$mydir" -c %U) ] then echo "The directory $mydir exists for user $myuser" \ "but is not ...


3

You could use a BEGIN block to set a flag unset the flag when your current test finds a missing directory check to see if the flag is still set in a END block and if so print your message e.g. awk -F: ' BEGIN{nores=1;} {if(system( "[ -d " $6 " ]") == 1 && $7 != "/sbin/nologin" ) {print "The directory " $6 " does not exist for user " $1; nores=0 ...


3

You know, I'm not convinced you necessarily need a repetitive feedback loop as your diagrams portray, so much as maybe you could use a persistent pipeline between coprocesses. Then again, it may be there isn't too much of a difference - once you open a line on a coprocess you can implement typical style loops just writing information to and reading ...


3

In general I would use a Makefile (command make) and try to map your diagram to makefile rules. f1 f2 : f0 command < f0 > f1 2>f2 To have repetitive/cyclic commands, we need to define a iteration policy. With: SHELL=/bin/bash a.out : accumulator cat accumulator <(date) > a.out cp a.out accumulator accumulator: touch ...


2

When you are printing straight to the terminal, your shell doesn't know about it, so it doesn't know to print its prompt again. You would get similar behavior running e.g. (sleep 1; echo foo) &. I would suggest either not printing from your udev rule (that seems like the more usual thing to do: be quiet unless something wrong happened), or living with ...


2

stdbuf examples at http://www.pixelbeat.org/programming/stdio_buffering/ nohup is used for any long running command that you want left running across logins. You can also do this with screen(1) or retroactively with screen + https://github.com/nelhage/reptyr


2

var=$(awk '/eth0:4/ {print $2}' file)"


2

As mentioned in comment you can use: read -t 1 -n 1 key which because of -t option we can remove sleep, so your script could be: #!/bin/bash for ((i=0; i<100; i++)); do read -t 1 -n 1 key if [ "$key" = "k" ]; then i=$((i + 10)) fi echo $i done But I think more portable could be: #!/bin/bash if [ -t 0 ]; then stty -echo ...


2

It is the back tick quotes. They tell the shell to run the output of the command. e.g. `echo ls` will run ls. In your case you have asked bash to run the sql files. This is obviously not what you intended, as bash can not do this, the sql will not make sense to bash. Also, even with this fix, the script will not do what you describe. Someone else ...


2

Or your awk could be: awk -F: 'BEGIN { FS = ":"; nores = 1; } { if ((system("[ -d " $6 " ]") == 0) && ($7 != "/sbin/nologin")) { "stat -c \"%U\" " $6 | getline s; if (s != $1) { print "The directory " $6 " exists for user " $1 " but is not owned by that user"; nores = 0 } } } END { if (nores) print "No results"; }' /etc/passwd


2

Although mikesrv solution is much better and safer I think this can be done also with rename: find Music/ -type f -name '*_000' -print0 | xargs -r0 rename -v '_000' '' But there are still potential problems as mentioned, for example album_00020_000 will be renamed to album20 which obviously is not the desired behaviour. I think on debian distribution was ...


2

cd Music/.. && mkdir Music2 && pax -'rwls|\([^/]\)_000$|\1|p' Music Music2 If run from Music's parent directory, the above commands will create a directory called Music2 then mirror Music's path structure in Music2 w/ hardlinks while removing any occurrence of _000 found at the tail of any filename therein. Afterward you'll have ...


2

You should not believe them if they tell you it cannot be done. You should believe them, however, if they tell you it's not easy. sed '\|*/|!{ s|/\*|\n&| #if ! */ repl 1st /* w/ \n/* h; s|foo|bar|g;/\n/!b #hold; repl all foo/bar; if ! \n branch G; s|\n.*\n||;:n #Get; clear difference; :new label n; ...


2

The simplest way is to just run command1 "hello world" || command2 "hello world" If the first command doesn't exist, the left hand side of the || wil fail so the command on the right will be run. I don't see why you need to test first. Just do, and if you fail, do something else. You can make that slightly better by ignoring error messages cause by a ...


2

If you want to use getopts (noted the "s") to get the command line arguments you can do something like while getopts "i:n:e:" OPT; do case "$OPT" in i) # do stuff with the i option ID="$OPTARG" ;; n) # do stuff with the n option ;; e) # do stuff with the e ...


1

If you want to let date create the standard date formatting you could do: $ date -d "$( date -d "Fri Apr 17 20:16:01 IST 2015" "+2016-%m-%d %T" )" But you can also use just one date instance and use the appropriate time format specifiers, as in: date -d "Fri Apr 17 20:16:01 IST 2015" "+%a %b %e %T %Z 2016" Or - since your date format has the year at ...


1

Use the find command to enumerate files in a directory and its subdirectories recursively. There are several ways to rename files. You can use the Linux rename command: find Music -depth -name '*_000' -exec rename _000 '' {} \; This removes the first occurrence of _000 in each file name. Beware that if you had files whose name contains _000 in the middle, ...


1

With perl -F: -anE 'if( (stat $F[5])[4] != $F[2] ) { say "$F[0]($F[2]) not own $F[5]" }' /etc/passwd you get almost what you wanted. In /etc/passwd: F0 = username F5 = home F2 = uid stat FileOrDir [4] is the uid of FileOrDir Add some more condition to tune it. Example: perl -F: -anE 'if( -d $F[5] and # F5 is a ...


1

This code does the comparison and informs you by printing a message on the console where you typed the command: [[ $(date +%Y%m%d -d $(< file)) == $(date +%Y%m%d) ]] && echo "wake up" You may want to replace the echo by any other command that notifies you, e.g., use the command mail.


1

To "loop through a bunch of [.sql] files and get their names" you'd just do: for f in *.sql do do_whatever_with_file "$f" done If you just want to list the files: ls *.sql If you want to test whether there are *.sql files existing, for example: if ls *.sql >/dev/null 2>&1 then echo sql files existing else echo no sql files fi


1

Use PROMPT_COMMAND. What you're trying to do is exactly what it's for. It's expanded before the prompt is evaluated. If you want to derive some of the prompt content from that code, set variables in PROMPT_COMMAND, turn on the promptvars variable, and include these variables in PS1. See Stateful bash function and Display Non-Zero Return Status in PS1 and ...


1

You basically want dstat to provide you with an average over a given time (say 120 secs). This works exactly the same as vmstat or ifstat, the first argument is the delay, which means the given time to provide averages over. Default is 1 second. So this should do: dstat -n 120 or for given interfaces eth0 and eth1, do: dstat -n -N eth0,eth1 120 or for ...



Only top voted, non community-wiki answers of a minimum length are eligible