Tag Info

New answers tagged

4

Digging around here, I understood from http://unix.stackexchange.com/a/56877/54067 (where both the question and answer are worded differently and the problem is not related to interactive input) that the reason for the problem is that the cp -i expects the user to give the interactive input confirmation via stdin but in the cat | while read loop stdin is ...


0

The following script should do the job. LOG_SIZE=500000 NUM_SEGM=2 while getopts "s:n:" opt; do case "$opt" in s) LOG_SIZE=$OPTARG ;; n) NUM_SEGM=$OPTARG ;; esac done shift $((OPTIND-1)) if [ $# == 0 -o -z "$1" ]; then echo "missing output file argument" exit 1 fi OUT_FILE=$1 shift NUM=1 while :; do dd bs=10 ...


0

There is a clever trick with tee and subshells: cat source.lst | tee >(doSomething.sh) >(somethingElse.sh) | somethingFinal.sh I've done this before pv -perl source.list | tee >(doSomething.sh) >(somethingElse.sh) | md5sum pv will give you a progress bar, an ETA, and a running line total. Then source.lst will be fed to doSomething.sh and ...


0

I'm not sure I'd call it an "answer" per se, but I was able to approximate what I was looking to do in a completely different way. Instead of nc, I've managed to get further along using a file descriptor on /dev/tcp. exec 3<>/dev/tcp/${host}/${port} echo -e "read?" >&3 cat <&3 | tee -a ${log} exec 3<&- exec 3>&- There are ...


1

Store the output of smartctl into a variable and use it as many times as you like. smartctl_output=$(smartctl -a /dev/sda) serial_number=$(printf %s "$smartctl_output" | awk 'sub(/^Serial Number: */,"")') printf %s "$smartctl_output" >"$serial_number.txt"


1

You could use another command (namely lsblk) to get only the serial number of that particular device (so no need to parse smartctl output): smartctl -a /dev/sdb > $(lsblk --nodeps -no serial /dev/sdb)


0

You could just store the output of smartctl -a in a temporary file and then do the double parse on that. This makes testing your script fast (generate the temporary file once, comment out the call to smartctl, adjust your parser until they are satisfactory, then uncomment the smartctl line. Alternatively you can use pee to pipe the output of smartctl -a ...


-5

Whats wrong with simple two line batch file? Like: Cmd1 >filespec Cmd2 <filespec Or cmd1 >filespec cmd2 filespec either way, the file is left in mass storage.


6

The already given answer is correct. But if your goal is to selectively read your verylongfile.txt w/ cmd2, sed might be another option. cmd1 | sed -e 'w verylongfile.txt' -e '/notinteresting/d' | cmd2 sed will write all of its input to the outfile, but only the bits that do not match the /notinteresting/ address to the pipe. Or you might negate the ...


15

As far as I know, cmd1 | tee verylong.txt | cmd2 will not hold the whole file in memory. In fact, if cmd2 was to wait too long before consuming its input, cmd1 might block on a write call and unblock only when cmd2 starts reading again. The reason for that is that there is a buffer for the pipe, and that buffer, by default, is limited to a certain ...


4

I you mean that someone may open 2 TCP connections to your machine, one to port 50505 and another to port 60606, send data on the first one intended to be fed to P and expect to read the output of P from the second TCP connection, then that would be: < /dev/null nc -q -1 -l 50505 | P | nc -l 60606 > /dev/null Or with socat: socat -u ...


-1

You don't need nc in order to work with ports. bash can do that itself: Bash handles several filenames specially when they are used in redirections, as described in the following table: /dev/fd/fd If fd is a valid integer, file descriptor fd is duplicated. /dev/stdin File descriptor 0 is duplicated. /dev/stdout File descriptor 1 is ...


2

The only file descriptor numbers that you can portably use in a shell script are 0 through 9 (i.e. a single digit). Quoting POSIX: Open files are represented by decimal numbers starting with zero. The largest possible value is implementation-defined; however, all implementations shall support at least 0 to 9, inclusive, for use by the application. These ...


3

On second thought, perhaps a more simple approach than my original answer (which I have reproduced below since that is what was originally accepted) is to change the file descriptor from 200 to something that is <9. For example, 5. So, change all three instances of 200 in your script to 5: #!/bin/sh case "$IFACE" in lo) # The loopback ...


0

I don't see anything wrong which makes me wonder if your Fluke is actually responding. These two netcat scripts show that there is nothing wrong with your syntax server: # server while true; do date; sleep 1; done | nc -l 10001 client: # client echo "hello?" | nc -v -i1 localhost 10001 >> test.out 2>&1 test.out [sri@tidevel netlink]$ ...


2

The problem is that the output redirection kills the file before less is even started: open("file", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3 dup2(3, 1) = 1 close(3) = 0 execve("/usr/bin/less", ["less", "-WNS", "file"], [/* 102 vars */]) = 0 The normal ourput redirection overwrites the file. The O_TRUNC (see man 2 ...


0

This operator implements a here document, which takes text from the following lines as standard input.Chances are you won’t use this redirector on the command line, though, because the following lines are standard input, so there’s no need to redirect them.Rather, you might use this command as part of a script in order to pass data to a command.Unlike most ...


1

This allows you to embed the text that will be fed to a command within a script in the script itself instead of an external file. So, instead of redirecting from another file, you could have the input content in a single file containing both the script and the input data. This is specially useful when mixing shell scripts and awk, perl or other scripting ...


0

this is call a here document. This is mainly use if you want to define a portion of a file in a command shell. I.e. cat <<foobarbaz > /tmp/file.txt hello world $$ foobarbaz will populate file /tmp/file.txt with hello world 1234 assuming 1234 is process ID.


0

You can use grep like: for f in /dir/*glob* do grep -E 'pattern|list' <<INFILE >"$f" $(cat "$f") INFILE done It will drop any trailing blank lines from each file in $f but my assumption was that blank lines were probably not among your pattern|list anyway.


0

using awk for x in `ls ~/map/*.xml`;do awk '/(pattern1|pattern2|pattern3|pattern4|pattern5)/{print}' $x > $x.2; done the for will loop over all xml file and awk find pattern , if matched will store in .xml.2


3

Edit in place, keeping only lines that match the pattern: sed -n -r -i "/(pattern1|pattern2|pattern3|pattern4|pattern5)/p" ~/mpp/*.xml On BSD sed (Mac OSX), try: sed -n -E -i '' "/(pattern1|pattern2|pattern3|pattern4|pattern5)/p" ~/mpp/*.xml Basing the file name off of an already existing variable for fname in ~/mpp/*.xml do egrep ...


1

If you want to hide all output from the commands you're running (both output and errors), but still be able to print messages yourself, then Hauke Laging's approach is absolutely the right way to do this. It lets you keep a reference to stdout (aka file descriptor 1) and stderr (aka file descriptor 2), redirect them to /dev/null, but still use them if you ...


7

You can save the file descriptors of stdout and stderr, overwrite them, and restore them after the programs have been run: exec 3>&1 exec 4>&2 exec 1>/dev/null exec 2>/dev/null ./script-1 ./script-2 ... ./script-n exec 1>&3 exec 2>&4 # delete the copies exec 3>&- exec 4>&-


8

Redirect all the output as a block. ( yum -y update service restart nginx yum install atop cat /var/log/somelog.log > /home/cron/php/script ) > /dev/null 2>&1


1

| mean pipe == output of the previous command piped to next command input. If you use > final.csv that redirect output of echo into file and remains nothing to pipe. So to correct you free use 2 command separatelly as you have offered above or use pipe if your want echo "some info" | cat - temp.csv > final.csv Other do the same cat - temp.csv ...


3

You are piping the output from echo "some info" > final.csv into cat temp.csv >> final.csv, so these two commands (echo and cat) run in parallel. Because of that, what ends up in the final.csv depends on what program gets scheduled. What you want to do is replace that | with a ;. And then the echo command will run until finished, and only then cat ...



Top 50 recent answers are included