Tag Info

Hot answers tagged

11

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

On a GNU system: find / -type d -print0 | shuf -zn5 | xargs -r0n1 cp foo (now copying the file to things like /sys or /proc would not make sense or even be possible, you may want to add -xdev to only select directories on the file system mounted at /). You could make it compatible with both FreeBSD and GNU with: find / -type d -print0 | sort -zR | tr ...


9

How about using cut? If you'd like to print the 2nd pattern echo "$above_string" | cut -f2 -d "?" Second column onward echo "$above_string" | cut -f2- -d "?"


7

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 ...


7

You can use Bash's parameter expansion: string="foo-bar-123" && printf "%s\n" "${string##*-}" 123 If you want to use another process, with Awk: echo "foo-bar-123" | awk -F- '{print $NF}' Or, if you prefer Sed: echo "foo-bar-123" | sed 's/.*-//' A lighter external process, as Glenn Jackman suggests is cut: cut -d- -f3 <<< "$string" ...


7

Here is what the bash documentation says: PS1 The value of this parameter is expanded (see PROMPTING below) and used as the primary prompt string. The default value is ``\s-\v\$ ''. PS2 The value of this parameter is expanded as with PS1 and used as the secondary prompt string. The default is ``> ''. PS3 The value ...


4

If I undersood the question correctly you need files in myfiles which do not have symlinks in images: #!/bin/bash OIFS="$IFS" IFS=$'\n' files="$(find myfiles/ -type f -name '*.jpg' -or -name '*.cr2')" for f in $files; do list="$(find -L images/ -xtype l -samefile "$f")" if [[ "$list" == "" ]]; then echo "$f does not have symlink." fi ...


4

There exists several variants of a watch command, some that spawn a shell to interpret a command line made of the concatenation of the arguments passed to watch (with space characters in between). In those you can do: watch 'ls | shuf' same as: watch ls '|' shuf (those watch actually run: "/bin/sh", ["sh", "-c", "ls | shuf"] and are quite dangerous in ...


4

Using Awk to print the second and third records separated by newlines: awk -F"?" '{printf "%s\n%s\n", $2,$3}' Elvis August 16 Leonard Nimoy February 27 If you want to swap out the record, you can set it as a variable: awk -v record=2 -F"?" '{print $record}' Elvis August 16


4

echo $above_string | grep -oP "^([^?]*\?){2}\K[^?]*" Change 2 to the n - 1 value in order to obtain the nth string. This assumes that you want the nth string in that line. You have n - 1 strings with no ? ending with a literal '?' (\? since it's a special character in perl regex). Then with \K you state you are not interested in the previous contents, ...


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 ...


3

To exclude specific paths, on Linux: find / -path /sys -prune -o -path /proc -prune -o -type d Another approach is to tell find not to recurse under different filesystems. find / -xdev -type d You could also use locate to query a database of file names (usually updated nightly) instead of the live system. locate '*' | shuf -n 1


3

You can check what locations are currently checked for direct commands by looking at the $PATH variable: echo $PATH It's likely this includes /usr/local/bin, in which case you could put a symbolic link there: ln -s /opt/mysuperscript /usr/local/bin/mysuperscript Now you can just type mysuperscript to run your script.


3

You're looking for tail : ls -ltr | tail -n 1 This will display only the last line of ls -ltr's output. You can control the number of lines by changing the value after -n; if you omit -n 1 entirely you'll get ten lines.


3

With sed you can do: sed '/\n/P;//d;s/[^?]*/\n&\n/[num];D' ...where you would replace the [num] above with some number representing the desired occurrence. If the numbered occurrence you specify does not exist, as is demonstrated in the following example, sed will simply print nothing at all. echo ,2,3 | sed '/\n/P;//d;s/[^,]*/\n&\n/4;D' Above ...


3

easier than fdisk for your purpose is lsblk: $ lsblk --nodeps NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 223.6G 0 disk sdb 8:16 0 298.1G 0 disk sr0 11:0 1 12M 0 rom or if you just want the drives: $ lsblk --nodeps -n -o name sda sdb sr0


2

There is no stand-alone read command: instead, it is a shell built-in, and as such is documented in the man page for bash: read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...] [...] -r Backslash does not act as an escape character. The back‐ slash is considered to be part ...


2

You should be able to use libreoffice in batch mode from the command line e.g. libreoffice --headless --convert-to doc *.odt or libreoffice --headless --convert-to docx *.odt


2

I'd use this kind of construct as a starting point find / -type d -print0 | xargs -0 -I'{}' sh -c 'ls -ltr {} | tail -1' Caveat: it doesn't like empty directories (total 0 is output).


2

Probably this would be better: combination of find and shell find / -type d -print0 | while read -r -d '' dir; do ls -ltr "$dir" | sed '$!d' done find will output each directory found, using the null byte instead of a newline to separate them. This stream is fed into a while loop, using read -d '' to extract each null-delimited directory name. Then, ...


2

You don't say what shell are you using. From the behaviour you are describing it's likely zsh. If you have a look in its man page you would notice how redirections are handled. Note that a pipe is an implicit redirection; thus cat bar | sort <foo is equivalent to cat bar foo | sort (note the order of the inputs). Otherwise, regular ...


2

Your understanding is not quite correct. In a | b the stdout output of process a connected through a pipe to stdin of process b. The problem with your code is that with an additional redirection from somefile to process b you will use two different methods at the same time to connect to stdin of process b. Don't do that! The question is; what do you try to ...


2

In addition to making sure the script is in the $PATH, you also must make the script executable. chmod +x SCRIPTNAME is how you do that.


2

This is a locale problem. watch uses strtod(3), which is locale-dependent, to convert the argument to -n to a double. To fix the problem, you need to either specify the argument to -n with a different separator: watch -n 0,1 w Or change your locale to a setting where the period character is used for the decimal point: export LC_NUMERIC=en_US.UTF-8 watch ...


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

grep -Po '(?<=\w-)\w+(?=$|[\s.,])'


2

None of the common unix shells (bash, [pd]ksh, [t]csh) have their own, internal analogue to cron in order to do automatic background tasks as you wish. But even if they did, after you've run a command and it has come back to the prompt, that character output to show the prompt is static, not dynamic. If you sat there and watched, it would not cycle through ...


2

I don't think it is possible, even if you incorporate the above pseudo-code in a shell script, because on each invocation of the shell script, it will start its own sub-shell where this stuff will happen and not where you want it to (your main shell). Even if you start it in the background. (script.sh &)


2

sed You can use sed for this, but it is not advisable, e.g. here is a zero-based solution that uses a quantifier to select the desired field: n=1 sed 's/\([^?]*? *\)\{'$n'\}//; s/?.*//' <<<"$above_string" Output: Elvis August 16



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