bash

Validating parameters to a bash script

I'm a total newbie to doing any bash scripting, but I came up with a basic one to help automate the process of removing a number of folders as they become unneeded. #!/bin/bash rm -rf ~/myfolder1/$1/anotherfolder rm -rf ~/myfolder2/$1/yetanotherfolder rm -rf ~/myfolder3/$1/thisisafolder This is evoked like so: ./myscript.sh <{id-numb...

Best way to choose a random file from a directory in a shell script

What is the best way to choose a random file from a directory in a shell script? Here is my solution in Bash but I would be very interested for a more portable (non-GNU) version for use on Unix proper. dir='some/directory' file=`/bin/ls -1 "$dir" | sort --random-sort | head -1` path=`readlink --canonicalize "$dir/$file"` # Converts to ...

How do I schedule a process' termination?

I need to run a process, wait a few hours, kill it, and start it again. Is there an easy way that I can accomplish this with Python or Bash? I can run it in the background but how do I identify it to use kill on it? ...

Calendar calculations in bash

Hi. I want to do some calendar manipulations in bash - specifically, I want to figure out the last date of a given month (including leap-year, and a preparing a table for a lookup is not a valid solution for me). Supposedly I have the following code: $year=2009 $start_month=2 $end_month=10 for $month in $(seq $start_month $end_month); ...

How do I alter tab autocomplete in bash to dive through folders?

I have a folder 'test' which contains another folder 'test2' When I type 'cd te[tab]' it auto-completes to 'cd test/' How do I make it autocomplete to 'cd test/test2/', without hitting tab again? To clarify: test is the only folder/file in the folder test. I want this to work recursively so if there is a folder/with/a/lot/of/single/fi...

How to remove files starting with double hyphen?

I have some files on my Unix machine that start with -- e.g. --testings.html If I try to remove it I get the following error: cb0$ rm --testings.html rm: illegal option -- - usage: rm [-f | -i] [-dPRrvW] file ... unlink file I tried rm "--testings.html" || rm '--testings.html' but nothing works. How can I remove such...

Why is this bash prompt acting strangely/disappearing, and how do I fix it (OS X)?

I admit that I use a somewhat long-winded bash prompt: --(username)-(Wed April 01|12:00:00)--(~ $ Recently, I got the bright idea to change it so that depending on the exit value from the previous command, if success, the interior elements of the ()'s would be green, and if failure, they would be red. I got it working for the most par...

How to run two processes as though they were one in bash?

hi, I've got two commands foo and bar. foo runs for a long time without stdin or stdout/stderr activity. bar is a client of foo and runs with stdout/stderr but no stdin activity. I'd like to run them from one shell, being able to kill both with ctrl-c, and to see the output from bar as it occurs. i.e. something like this sequence fo...

Synchronize home directories from multiple clients to a server

I'm using multiple Linux laptops/desktops and want them to "share" home directories. NFS is unfortunately not an option. Therefor I was trying to create a bash script using rsync but I can't figure out how to do it. This is my example right now #!/bin/bash sync() { rsync -azvR --exclude-from=/home/ME/.rsync_excludes --delete -e 's...

Bash - Suppress Notice of Forked Command Being Killed

Let's suppose I have a bash script (foo.sh) that in a very simplified form, looks like the following: echo "hello" sleep 100 & ps ax | grep sleep | grep -v grep | awk '{ print $1 } ' | xargs kill -9 echo "bye" The third line imitates pkill, which I don't have by default on Mac OS X, but you can think of it as the same as pkill. Howeve...

using the passwd command from within a shell script

I'm writing a shell script to automatically add a new user and update their password. I don't know how to get passwd to read from the shell script instead of interactively prompting me for the new password. My code is below. adduser $1 passwd $1 $2 $2 ...

run the output of a script as a standalone bash command

suppose you have a perl script "foobar.pl" that prints the following to stdout date -R and you want to run whatever that perl script outputs as a standalone bash command (don't worry about security problems as this is running in a trusted environment). How do you get bash to recognize this as a standalone command? I've tried using x...

How can I generate a file of random negative and positive integers in serial?

I want a file of randomly generated positive or negative serial integers. For now, I ask the file contain roughly (no guarantee required) equal numbers of negative and positive, but make it easy to change the proporotions later. By "serial", I mean the kth random negative is equal to -k, and the kth random positive is equal to +k. Thi...

Bash TAB-completion inside double-quoted string

Problem I'm writing a Twitter client for the command line (in C). I'm currently working on doing TAB-completion for Twitter screen names, like so: tweet "@s<TAB> @sourcebits @spolsky However, I can't get it to work mid-string, e.g.: tweet "Foo bar @s<TAB> since Bash treats the string as one word. I couldn't find anythin...

Can I use cygwin to script a hudson build step?

I've tried executing the following: #!C:\cygwin\bin\bash.exe ls ${WORKSPACE} But that doesn't find ls (even if it's on the windows path). Is there any way to set this up? UPDATE: In other words, I want to be able to set up a build step that uses cygwin bash instead of windows cmd like this page shows you how to do with Python. ...

Question about bash script: tar -cvf /dev/nst0 /home/user1 >> file1.log

Hi I need help to understand the following command. tar -cvf /dev/nst0 /home/user1 >> file1.log Thanks! ...

How can I write a linux bash script, that tells me which computer are on in my LAN ?

How can I write a linux bash script, that tells me which computers are ON in my LAN ? It would help if I could give it as input a range of IP's. ...

How to run a .sh-script in an Unix console/Mac terminal?

I know it, forgets it and relearn it again. Time to write it down. ...

Perl's diamond operator: can it be done in bash?

Is there an idiomatic way to simulate Perl's diamond operator in bash? With the diamond operator, script.sh | ... reads stdin for its input and script.sh file1 file2 | ... reads file1 and file2 for its input. One other constraint is that I want to use the stdin in script.sh for something else other than input to my own script. Th...

How to get CURL to save to a different directory?

I need to be able to pass in the URL of the file download, plus a path for the file to be saved to. I think it has something to do with -O and -o on CURL, but I can't seem to figure it out. For example, this is what I'm using in my bash script now: #!/bin/sh getsrc(){ curl -O $1 } getsrc http://www.apache.org/dist/ant/binaries/a...