How do I join two named pipes into single input stream in linux

Personally, my favorite (requires bash and other things that are standard on most Linux distributions) The details can depend a lot on what the two things output and how you want to merge them … Contents of command1 and command2 after each other in the output: cat <(command1) <(command2) > outputfile Or if both commands … Read more

Adding a directory to $PATH in CentOS?

It’s not a good idea to edit /etc/profile for things like this, because you’ll lose all your changes whenever CentOS publishes an update for this file. This is exactly what /etc/profile.d is for: echo ‘pathmunge /usr/lib/ruby-enterprise/bin’ > /etc/profile.d/ chmod +x /etc/profile.d/ Log back in and enjoy your (safely) updated $PATH: echo $PATH /usr/lib/ruby-enterprise/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin which ruby … Read more

What’s the difference betwen the single dash and double dash flags on shell commands?

A single hyphen can be followed by multiple single-character flags. A double hyphen prefixes a single, multicharacter option. Consider this example: tar -czf In this example, -czf specifies three single-character flags: c, z, and f. Now consider another example: tar –exclude In this case, –exclude specifies a single, multicharacter option named exclude. The double hyphen … Read more

How can I kill all stopped jobs?

To quickly kill all the stopped jobs under the bash, enter: kill -9 `jobs -ps` jobs -ps lists the process IDs (-p) of the stopped (-s) jobs. kill -9 `jobs -ps` sends SIGKILL signals to all of them.

Colors in bash after piping through less?

Most likely your ls is aliased to ls –color=auto, which tells ls to only use colors when its output is a tty. If you do ls –color (which is morally equivalent to ls –color=always), that will force it to turn on colors. You could also change your alias to do that, but I wouldn’t really … Read more

How to add a timestamp to bash script log?

You can pipe the script’s output through a loop that prefixes the current date and time: ./ | while IFS= read -r line; do printf ‘%s %s\n’ “$(date)” “$line”; done >>/var/log/logfile If you’ll be using this a lot, it’s easy to make a bash function to handle the loop: adddate() { while IFS= read -r … Read more

What does ‘set -e’ do, and why might it be considered dangerous?

set -e causes the shell to exit if any subcommand or pipeline returns a non-zero status. The answer the interviewer was probably looking for is: It would be dangerous to use “set -e” when creating init.d scripts: From 9.3.2 — Be careful of using set -e in init.d scripts. Writing correct init.d scripts requires … Read more