bash

How to extract and chop version string from file in bash

Hi all I have a simple text file ./version containing a version number. Unfortunately, sometimes the version number in the file is followed by whitespaces and newlines like 1.1.3[space][space][newline] [newline] [newline] What is the best, easiest and shortest way to extract the version number into a bash variable without the traili...

Read characters from a text file using bash

Does anyone know how I can read the first two characters from a file, using a bash script. The file in question is actually an I/O driver, it has no new line characters in it, and is in effect infinitely long. ...

shell script to spawn processes, terminate children on SIGTERM

I want to write a shell script that spawns several long-running processes in the background, then hangs around. Upon receiving SIGTERM, I want all the subprocesses to terminate as well. Basically, I want a "master process". Here's what I got so far: #!/bin/sh sleep 600 & PID1="$!" sleep 600 & PID2="$!" # supposedly this should kill...

Colorized grep -- viewing the entire file with highlighting

I find grep's --color=always flag to be tremendously useful. However, grep only prints lines with matches (unless you ask for context lines). Given that each line it prints has a match, the highlighting doesn't add as much capability as it could. I'd really like to cat a file and see the entire file with the pattern matches highlighted....

capturing standard error into a variable in bash

...

pushd/popd on ksh?

Is there an equivalent of the bash pushd/popd build in commands for the KSH? For those who don't know what pushd and popd in bash do, here is the description from the man page pushd [-n] [dir] pushd [-n] [+n] [-n] Adds a directory to the top of the directory stack, or rotates the stack, making the new top of ...

GNU Screen running a bash init script

I'm sure there is an answer to this in the manual to screen, but I can't find it! I want the bash shell spawned by GNU screen to source in a file in addition to the .bashrc that it already runs. I can't put a call to the file in .bashrc because on our site .bashrc files get regenerated automatically on login. Any ideas? EDIT: I creat...

Protect against accidental deletion

Today I first saw the potential of a partial accidental deletion of a colleague's home directory (2 hours lost in a critical phase of a project). I was enough worried about it to start thinking of the problem ad a possible solution. In his case a file named '~' somehow went into a test folder, which he after deleted with rm -rf... when ...

Can I chain multiple commands and make all of them take the same input from stdin?

In bash, is there a way to chain multiple commands, all taking the same input from stdin? That is, one command reads stdin, does some processing, writes the output to a file. The next command in the chain gets the same input as what the first command got. And so on. For example, consider a large text file to be split into multiple files...

bash - redirecting of stdoutput and stderror does not catch all output

I am writing some testing scripts and want to catch all error output and write it to an error log as well as all regular output and write that to a separate log. I am using a command of the form cmd > output.file 2> error.file The command I am writing test scripts for can cause a segmentation fault. When the command segfaults, bash s...

Running a command in a new Mac OS X Terminal window.

Hi, I've been trying to figure out how to run a bash command in a new Max OS X Terminal.app window. As, an example, here's how I would run my command in a new bash process: bash -c "my command here" But this reuses the existing terminal window instead of creating a new one. I want something like: Terminal.app -c "my command here" Bu...

can "nice" and "exec" cooperate in linux?

This one fails: nice -n 10 exec "$JAVA" $JAVA_HEAP_MAX $NUTCH_OPTS -classpath "$CLASSPATH" $CLASS "$@" this succeeds: nice -n 10 java test Does it mean can't combine nice and exec? ...

How does this bash fork bomb work?

According to Wikipedia, the following is a very elegant bash fork bomb: :(){ :|:& };: How does it work? ...

Safe rm -rf function in shell script

This question is similar to http://stackoverflow.com/questions/373156/what-is-the-safest-way-to-empty-a-directory-in-nix I'm writing bash script which defines several path constants and will use them for file and directory manipulation (copying, renaming and deleting). Often it will be necessary to do something like: rm -rf "/${PATH1}"...

Algebraic logic

Both Wolfram Alpha and Bing are now providing the ability to solve complex, algebraic logic problems (ie "solve for x, given this equation"), and not just evaluate simple arithmetic expressions (eg "what's 5+5?"). How is this done? I can read most types of code that might get thrown at me, so it doesn't really make a difference what yo...

Python regular expression with [:numeric:]

Hi there, I am having some trouble with Python giving me a result I do not expect. Here is a sample code : number = re.search(" [0-9] ", "test test2 test_ 2 333") print number.groups() number = re.search(" [[:digit:]] ", "test test2 test_ 2 333") print number.groups() In the first block I get an object returned but with nothing in it...

right align/pad numbers in bash

What's the best way to pad numbers when printing output in bash, such that the numbers are right aligned down the screen. So this: 00364.txt with 28 words in 0m0.927s 00366.txt with 105 words in 0m2.422s 00367.txt with 168 words in 0m3.292s 00368.txt with 1515 words in 0m27.238 Should be printed like this: 00364.txt with 28 words...

Redirecting console output to a Python string

Possible Duplicate: How can I capture the stdout output of a child process? I'm running a cat-like program in bash from Python: import os os.system('cat foo.txt') How do I get the output of the shell command back in the Python script, something like: s = somefunction('cat foo.txt') ? UPD: Here is a related threa...

Bash: Delete until a specific file

Hy, suppose you got many files in a folder and you want first to sort it in alphabetic order and then delete all files until a specific file (not including it). So im searching for a function/command/script/whatever which takes one string as imput and the deletes all files until this file. I thought of a simple bash-script: for i in *...

meaning of tilde in linux bash (not home directory)

Hi all, First off, I know that ~/ is the home directory. CDing to ~ or ~/ takes me to the home directory. However, cd ~X takes me to a special place, where X seems to be anything. In bash, if I hit "cd ~" and hit tab, it shows a bunch of possible ~X options like ~mail and ~postgres and ~ssh. Going to those folders and doing a pwd sh...