bash

Why doesn't **sort** sort the same on every machine?

Using the same sort command with the same input produces different results on different machines. How do I fix that? ...

How do you use ssh in a shell script?

When I try to use an ssh command in a shell script, the command just sits there. Do you have an example of how to use ssh in a shell script? ...

Getting ssh to execute a command in the background on target machine

This is a follow-on question to the How do you use ssh in a shell script? question. If I want to execute a command on the remote machine that runs in the background on that machine, how do I get the ssh command to return? When I try to just include the ampersand (&) at the end of the command it just hangs. The exact form of the comman...

How to redirect all stderr in bash?

Hey! I'm looking for a way to redirect all the stderr streams in interactive bash (ideally to its calling parent process). I don't want to redirect stderr stream from each individual command, which I could do by appending 2> a_file to each command. By default, these stderr streams are redirected to the stdout of an interactive bash. I...

How do I make bash reverse-search work in Terminal.app without it displaying garbled output?

Using Terminal.app on OS X 10.5, often you see the commands get garbled when you do a reverse-search with Bash. Is there some kind of termcap or perhaps a bash shopt command that can fix this? It is very annoying. Steps to reproduce: Open Terminal.app, reverse-search to a longish command. Hit <ctrl>-E once you've found the command...

How do I autorun an application in a terminal in Ubuntu?

I've created a few autorun script files on various USB devices that run bash scripts when they mount. These scripts run "in the background", how do I get them to run in a terminal window? (Like the "Application in Terminal" gnome Launcher type.) ...

Parallelize Bash Script

Lets say I have a loop in bash: for foo in `some-command` do do-something $foo done do-something is cpu bound and I have a nice shiny 4 core processor. I'd like to be able to run up to 4 do-something's at once. The naive approach seems to be: for foo in `some-command` do do-something $foo & done This will run all do-somethin...

How to detect file ends in newline?

Over at Can you modify text files when committing to subversion? Grant suggested that I block commits instead. However I don't know how to check a file ends with a newline. How can you detect that the file ends with a newline? ...

How to run gpg from a script run by cron?

I have a script that has a part that looks like that: for file in `ls *.tar.gz`; do echo encrypting $file gpg --passphrase-file /home/$USER/.gnupg/backup-passphrase \ --simple-sk-checksum -c $file done For some reason if I run this script manually, works perfectly fine and all files are encrypted. If I run this as cron job, e...

Quick ls command

I've got to get a directory listing that contains about 2 million files, but when I do an "ls" command on it nothing comes back. I've waited 3 hours. I've tried "ls | tee directory.txt", but that seems to hang forever. I assume the server is doing a lot of inode sorting. Is there any way to speed up the ls command to just get a directory...

How to make a pipe loop in bash

Assume that I have programs P0, P1, ...P(n-1) for some n > 0. How can I easily redirect the output of program Pi to program P(i+1 mod n) for all i (0 <= i < n)? For example, let's say I have a program square, which repeatedly reads a number and than prints the square of that number, and a program calc, which sometimes prints a number af...

Maximum number of inodes in a directory?

Is there a maximum number of inodes in a single directory? I have a directory of 2 million+ files and can't get an the ls command to work against that directory. So now I'm wondering if I've exceeded a limit on inodes in Linux. Is there a limit before a 2^64 numerical limit? ...

Worth switching to zsh for casual use?

The default shell in Mac OS X is bash, which I'm generally happy to be using. I just take it for granted. It would be really nice if it auto-completed more stuff, though, and I've heard good things about zsh in this regard. But I don't really have the inclination to spend hours fiddling with settings to improve my command line usage by a...

Boolean Expressions in Shell Scripts

What's the "right" way to do the following as a boolean expression? for i in `ls $1/resources`; do if [ $i != "database.db" ] then if [ $i != "tiles" ] then if [ $i != "map.pdf" ] then if [ $i != "map.png" ] then svn export -q $1/resources/$i ../MyProject/Resources/$i ... ...

How do I write a for loop in bash

I'm looking for the basic loop like: for(int i = 0; i < MAX; i++) { doSomething(i); } but for bash. ...

How do you parse a filename in bash?

I have a filename in a format like: system-source-yyyymmdd.dat I'd like to be able to parse out the different bits of the filename using the "-" as a delimiter. ...

Bash script to pad file names

What is the best way, using Bash, to rename files in the form: (foo1, foo2, ..., foo1300, ..., fooN) With zero-padded file names: (foo00001, foo00002, ..., foo01300, ..., fooN) ...

Converting scripts from ksh to bash.

I have some ksh scripts which I'd like to convert to run with bash instead. Are there any useful on-line resources for this? I'm really looking for a list of differences between the two shells and any gotchas I might encounter, although all information is welcome :-) ...

Using the result of a command as an argument in bash?

To create a playlist for all of the music in a folder, I am using the following command in bash: ls > list.txt I would like to use the result of the pwd (print working directory) command for the name of the playlist. Something like: ls > ${pwd}.txt That doesn't work though - can anyone tell me what syntax I need to use to do somet...

How to check if a directory exists in a bash shell script

What command can be used to check if a directory does or does not exist, within a bash shell script? ...