Tag Info

New answers tagged

8

Use script(1) to log everything sent to the terminal: $ script Script started, file is typescript $ # do your work ... $ # then exit with ^D $ exit Script done, file is typescript You can later look at the output with less: $ less -r typescript Beware that the logs will contain all control characters sent to the terminal, such as ANSI colours or ...


0

I've since tried tackling the problem from a different angle, using the 'expect ' command. However the following fails to work: #!/usr/bin/expect spawn sudo mount.cifs "//192.168.1.2/My Pictures" /home/pi/Desktop/Pictures -o user=Rob_ expect "Password: " { set send_slow {1 .1} send -s "a_password" } It responds to the password prompt by typing it ...


-1

There are multiple reasons: Your debug printf didn't work, because mount prints its prompt to the terminal, not stdout. read prompt failed and the loop was never entered. You are trying to echo the password to /dev/stdin. This will not work, you can only read from it. the mount's stdin is still connected to the terminal. There is no way to emulate user ...


1

Use this with bash: TEST='{"foo": "bar"}' PB_SIG=$(jq '.foo' <<< "$TEST") echo "$PB_SIG" Output: "bar"


0

Try this on the second line: PB_SIG=`echo $TEST | jq '.foo'` The two problems I see is that you need to echo the $TEST variable's value through the pipe and that you need to capture the output of the piped command.


4

Okay, let's break this down. A subshell executes its contents in a chain (i.e., it groups them). This actually makes intuitive sense as a subshell is created simply by surrounding the chain of commands with (). But, aside from the contents of the subshell being grouped together in execution, you can still use a subshell as if it were a single command. That ...


1

subshell (command) will execute command in subshell, this is usefull, if you have more then one command. (ls) | wc will pipe ls to wc, obviously you can write ls | wc (ls ; date) | wc will pipe the result of both ls and date to wc. using ls ; date | wc will result in only date being piped to wc. substitution $(command) will execute command and ...


2

You don't have to get the whole lump if you don't want to - you can chunk out stdin and work it as a stream if you like. I did some googling, though, and I don't think I can offer you any advice on how to change the sex of your script, or whatever it is you meant by polymorphic. In any case, I typically find that putting the whole of input aside in some ...


3

You can read from stdout by redirecting input from file descriptor 1. Stdout is file descriptor 1 by definition. The fact that file descriptor 1 is used for output is a matter of convention, not a technical obligation. However it's a bizarre thing to do which is bound to confuse the people who use your script. read line <&1 If you want to read a ...


2

If you want to read all of stdin into a shell script, usually you just capture it into a temp file: TMPFILE=$(mktemp /tmp/$0.$$.XXXXXX) cat > "$TMPFILE" # Script works with $TMPFILE and its contents, # ultimately writing everything to stdout. rm -f "$TMPFILE" Even system utilities do things very much like this. sort has to have all of stdin before it ...


3

A pipe sends its output to the program that has it open for reading. In a shell pipeline, that's the program on the right-hand side of the pipe symbol, i.e. evince in your example. You're sending the file name tmp.pdf to evince on its standard input. However evince doesn't care about its standard input. Like every program that acts on a file, it expects the ...


1

I can't find a way with the pipe but you can try this: obexftp -b 10:68:3F:57:7D:B6 - p $(cat inputFile.tar) Return the sdout of the command in the $( )


2

That's the effect of MULTIOS. echo foo >&2 | grep foo will write foo to stderr and also pipe foo to grep. Because stderr defaults to terminal, you will see two foo lines, one from echo, one from the grep result. { echo foo >&2 | grep foo } >/dev/null Here, you saw one line because stdout was redirected to /dev/null, you only saw the ...


6

Your understanding is correct. The sequence command1 | command2 is sending the output (STDOUT) of command1 to the input (STDIN) of command2. The reason your evince command didn't work is that evince doesn't accept a filename on STDIN.


2

The second awk reads two inputs, one after the other - from the piped output of the first awk and then from the file itself. One way to identify the start of the second input is that NR (the Number of the current input Record, overall) no longer matches FNR ( the current File's record number). Note that - as a FILE arg means tells awk to get the data from ...


0

I stumbled over tea4cups (in Debian the Package is cups-tea4cups), where one can do exactly what I want, like this: # tea4cups.conf [myprinter] # just the cups printer name filter: mycommand # pipes everything though mycommand, like "<input> | mycommand | lp" # if the printer URI is prefixed with 'tea4cups://'


2

Yes, OpenSolaris includes the Bourne Shell source but that source is not portable. A maintained and highly portable version of the Bourne Shell source can be found here in the schily-*.tar.bz2 archives. Here is the related part of the source in cmd.c: /* * ^ is a relic from the days of UPPER CASE ONLY tty model 33s */ if ((t = item(TRUE)) != 0 ...


1

The commands you're using can only extract data from the first entry in a ZIP archive; this is mentioned explicitly in the funzip manpage: funzip without a file argument acts as a filter; that is, it assumes that a ZIP archive (or a gzip'd(1) file) is being piped into standard input, and it extracts the first member from the archive to stdout. ...


1

cat /proc/uptime | cut -f1 -d' ' is correct < /proc/uptime cut -f1 -d' ' is correct and more efficient as it reads from /proc/uptime directly without creating a pipe (not that it matters here much). It's generally advisable to use the second form on forums, or else you'll get purists coming after you shouting "useless use of cat". cut -f1 -d' ' ...


0

I think there is some timing involved here. mkfifo /tmp/pipe echo >/tmp/pipe (shell process is hung) It doesn't matter which gets started first, because the writer will block anyway until a reader opens its end. And so, because practically any sane Unix program will init i/o before seeing to anything else, ps will hang until the grep process is ...


0

By default the delimiter for cut is TAB, you could change it to be whitespace with cat /proc/uptime | cut -f1 -d " " Although in those cases I prefer to use awk: cat /proc/uptime | awk '{print$1}'


5

The default field delimiter for cut is a tab. Since your file has a space instead, you need to specify the delimiter: -d ' ' And you really don't need to use cat or a pipe at all. Just read the file directly. cut -f 1 -d ' ' /proc/uptime


2

Scheduling doesn't matter much at all. If you consider piping an hour long job, the pipe remains open continuously from when the first program is started until it finishes. The second program runs exactly the same time as the first one and both finish at the same time (in general).. In other words, ps does not get the output then send it to grep - the two ...


2

This question is a duplicate and belongs to unix.stackexchange.com. To sum up, still, the OpenGroup's Shell Command Language doc is relatively vague on details regarding "pipelines": A pipeline is a sequence of one or more commands separated by the control operator '|'. The standard output of all but the last command shall be connected to the standard ...


2

With any POSIX sed you can achieve line-buffered output w/ the write command. while sleep 1 do upower -d done | sed -n /percentage/w\ /dev/fd/1 | uniq ...will work on systems supporting the /dev/fd/[num] links (such as practically any linux system). But you could just do: while sleep 1 do upower -d done | sed '/percentage/!d;H;x ...


7

In addition to Marco d'Itri's answer, not every tool supports custom buffering. In case the tool you're running doesn't, or in case you want to run it using a custom buffer size, you can use stdbuf to override the tool's buffering behavior; in this case, for example, to force the output to be line-buffered: while true ; do upower -d ; sleep 1 ; done | ...


9

You should use grep --line-buffered percentage or else it will take a very long time for the grep stdout buffer to be filled by its output.


1

The error message means that the reading end of the pipe has closed and openssl can no longer write to the pipe. This will happen, for example, if you pipe to a command that terminates quickly without reading all its input: $ dd count=400 if=/dev/urandom | openssl enc | true error writing output file Normally, a simple grep that finds nothing will work: ...


2

You are almost there. In your last command, you can use -I to do the ls correctly -I replace-str Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1. So, with find . ...


2

You can select the lines between a range in awk (this assumes you know how many lines there are): awk 'NR>1 && NR < 3' file Or in Perl: perl -ne 'print if $.>1 && $.<3' file If you don't know how many lines there are, you can calculate it on the fly using grep (note that this will not count blank lines, use grep -c '' file ...


1

There are several ways to remove leading and trailing line(s) from a file. You can use awk as it handles both pattern matching and line counting, #you need to know length to skip last line, assume we have 100 lines awk 'NR>1 && NR<100 {print $0};' < inputfile #or awk '/SQL/ {next}; {print $0;};' < inputfile |awk 'NR>1&& ...


2

Using awk: < inputfile awk 'NR>1 {print r}; {r=$0}' > outputfile < inputfile: redirects the content of inputfile to awk's stdin > outputfile: redirects the content of awk's stdout to outputfile NR>1: executes the following actions only if the number of the record being processed is greater than 1 {print r}: prints the content of the ...


2

You'd be far better served by cutting away the SQL commands. You can do this in two ways: If you are absolutely sure that the sequence "SQL>" does not occur anywhere else in the output, grep -v -F 'SQL> ' < infile > outfile If you aren't as sure, grep -v '^SQL> .*;$' < infile > outfile The second version is slower but more ...


3

ed is 'the standard text editor' and should be available on systems that don't have GNU sed. It was originally designed as a text editor, but it is well-suited to scripting. printf '%s\n' 1d '$d' w q | ed Element_query 1d deletes the first line of the file, $d (quoted so that the shell doesn't think it's a variable) deletes the last line, w writes the ...


2

I'm not going to answer how to delete a number of lines. I'm going to attack the problem this way: grep -v '#SQL>' Element_query >outfile Instead of counting lines, it eliminates the SQL commands by recognizing the prompts. This solution can then be generalized for other output files of SQL sessions with more commands than just two.


5

head; head { head -n[num] >/dev/null head -n[num] } <infile >outfile With the above you can specify the first number of lines to strip off of the head of the output w/ the first head command, and the number of lines to write to outfile with the second. It will also typically do this faster than sed - especially when input is large - ...


12

Using GNU sed: sed -i '1d;$d' Element_query How it works : -i option edit the file itself. You could also remove that option and redirect the output to a new file or another command if you want. 1d deletes the first line (1 to only act on the first line, d to delete it) $d deletes the last line ($ to only act on the last line, d to delete it) Going ...


2

sort can take multiple input files (and has a built-in uniq equivalent -u). Combine that with a fancy bash process substitution to result in: sort -u <(tshark -r sample.pcap -T fields -e eth.src -e ip.src) <(tshark -r sample.pcap -T fields -e eth.dst -e ip.dst) > hello_uniq


2

(tshark -r sample.pcap -T fields -e eth.src -e ip.src; tshark -r sample.pcap -T fields -e eth.dst -e ip.dst) | sort | uniq > hello_uniq


3

Make the target log file a named pipe, with your filtering script on the other side.


2

The yash shell has unique features (pipeline redirection and process redirection) that make that easier there: cat myfile | ( exec 3>>|4 tee /dev/fd/3 3>(commanda) | commandb 3>&- | paste -t',' /dev/fd/4 - 3>&- ) 3>>|4 (pipeline redirection) creates a pipe where the writing end is on fd 3 and the reading end on fd ...



Top 50 recent answers are included