bash

How to glob variables in bash script?

When the following two lines of code are executed in a bash script, "ls" complains that the files don't exist: dirs=/content/{dev01,dev02} ls -l $dirs When I run the script with the -x option, it appears to be passing the variable within single quotes (which would prevent globbing): + dirs=/content/{dev01,dev01} + ls -l '/content/{de...

Command line to delete all ClearCase view-private files

I'm looking for a command line to remove all view-private files and directories from a ClearCase view on Windows. I have Cygwin available as well. The script available at this article is not quite what I want, since I'm working with a large number of files and want to delete them all without having to select each one. ...

How to trim whitespace from bash variable?

I have a shell script with this code: var=`hg st -R "$path"` if [ -n "$var" ]; then echo $var fi But the conditional code always executes because hg st always prints at least one newline character. Is there a simple way to strip whitespace from $var (like trim() in php)? or Is there a standard way of dealing with this issue?...

What is the most elegant way to remove a path from the $PATH variable in Bash?

Or more generally, how do I remove an item from a colon-separated list in a Bash environment variable? I thought I had seen a simple way to do this years ago, using the more advanced forms of Bash variable expansion, but if so I've lost track of it. A quick search of Google turned up surprisingly few relevant results and none that I wo...

What is the best way to write a wrapper function that runs commands and logs their exit code

I currently use this function to wrap executing commands and logging their execution, and return code, and exiting in case of a non-zero return code. However this is problematic as apparently, it does double interpolation, making commands with single or double quotes in them break the script. Can you recommend a better way? Here's the...

Avoid expansion of * in bash builtin function let

Hi all, I have a problem with a bash script. I have to use the operator * to multiplicate. Instead the script bugs me with expansion and using as operator the name of the script itself. I tried with single quotes but it doesn't work :( Here's the code #!/bin/bash -x # Bash script that calculates an arithmetic expression # NO PRECEDENCE...

Get name of a variable as input and change the variable with that name

I get the name of a variable from the script user as the first argument and I echo the value of said variable back to the console: #!/bin/bash variablename=$1 echo "The value of $variablename is: " ${!variablename} This works great! What I can't get to work is if I want to change that variable into the value of the second argument fr...

darcs help breaks my shell

I have just installed darcs 2.1.2.2. When I type darcs help, sth less-like shows up. When I dismiss it with q it goes away but I don't get prompt and can't execute any commands. C-c doesn't work either. I am using bash on gentoo. ...

How to increment a number in several files if the number is not always the same ?

Hi, I have several files containing this line Release: X I want to increment X in all the files. If X was constant between the files, I could have a bash script looping around the files and doing ($1 containing the former release number and $2 the new one, ie. $1 + 1) : sed 's/Release: '$1'/Release: '$2'/' <$file >$file.new Now,...

IDE that provide autocompletion and error detection for Linux bash or shell scripting?

Our dev team is looking for an IDE like vi or nano or even textpad for windows that has the capability to autocomplete and error correction for bash or shell script for linux. Basically something similar to .NET autocompletion where you will be able to see if an if[ $# -ne 5 ]; then has no space between the 5 and the ] will tell yo...

bash script to show compatible commands based on "Windows-speak"

Problem: Customer X is a Windows user who wants to be able to trigger pre-packaged bash commands by using mnemonic keywords or "tag hints" when she is logged in to her RedHat box via shell. Example: Customer X logs into host using ssh and wants to do some routine file operations. She wants to be able to type copy file and get back a ...

Best way to simulate "group by" from bash

Suppose you have a file that contains IP addresses, one address in each line: 10.0.10.1 10.0.10.1 10.0.10.3 10.0.10.2 10.0.10.1 You need a shell script that counts for each IP address how many times it appears in the file. For the previous input you need the following output: 10.0.10.1 3 10.0.10.2 1 10.0.10.3 1 One way to do this i...

Get program execution time in the shell

I want to execute something in a linux shell under a few different conditions, and be able to output the execution time of each execution. I know I could write a perl or python script that would do this, but is there a way I can do it in the shell? (which happens to be bash) ...

NSTask not picking up $PATH from the user's environment

I don't know why this method returns a blank string: - (NSString *)installedGitLocation { NSString *launchPath = @"/usr/bin/which"; // Set up the task NSTask *task = [[NSTask alloc] init]; [task setLaunchPath:launchPath]; NSArray *args = [NSArray arrayWithObject:@"git"]; [task setArguments:args]; // Set the...

How do I control a job's "name" in Bash?

I'm trying to control the titles of my xterm windows and my cleverness has finally outpaced my knowledge. :-) I have three functions: one which sets the title of the window; one which takes a passed command, calls the title function, and executes the command; and one which resumes a job after using jobs to determine the title: title (...

Best way to kill all child processes

I basically want to kill a whole process tree. What is the best way to do this using any common scripting languages. I am looking for a simple solution. ...

Detect OS from a bash script

I would like to keep my .bashrc and .bash_login files in version control so that I can use them between all the computers I use. The problem is I have some OS specific aliases so I was looking for a way to determine if the script is running on OS X, Linux or Cygwin. What is the proper way to detect the operating system in a bash script...

Any interesting uses of Makefiles to share?

"make" is not only useful for building your programming project, but it seems to be under-used in other areas. For example, many shell scripts can be rewritten as Makefiles to allow independent parts to run in parallel (with "make -jXX") to keep all your CPU cores busy, with the explicitly declared dependencies as an added benefit in ca...

Auto complete by end/middle of line in bash

In bash is there a quick way to do tab auto-completion based on the middle of a word. So for example, if I have these files in a directory: 001_apple.txt 002_pear.txt 003_dog.txt I would like to type the sequence: *d<TAB> to auto-complete 003_dog.txt. Can this be done in bash? Is it easier to do in other shells? ...

The right components for an install script?

I am writing a bash script to set up a production server. The tasks at hand include compiling software, creating users and directories, copying files over, etc. I am wondering what other things I should do. Perhaps logging to a file? checking for a 0 exit status? What can I add to actually make this script great and not just "it wor...