bash

bash: Using sed in backticks

I want to escape some special chars inside a string automatically. I thought of echoing that string and pipe it through some seds. This doesn't seem to work inside of backticks. So why does echo "foo[bar]" | sed 's/\[/\\[/g' return foo\[bar] but FOO=`echo "foo[bar]" | sed 's/\[/\\[/g'` && echo $FOO just returns foo[bar] ? ...

How to define hash tables in bash?

Just what title says. I am surprised by insufficiency of results in Google search for this question! What I want to is the equivalent of Python dictionaries but in bash (and hence, should work across OSX, Ubuntu and other major Linux distributions). ...

Installing java on linux using ssh

Hi I want to install java on many computers using ssh so I want to write a bash script that will do (roughly): for c in computers do scp jre--.rpm $c ssh $c 'sudu -s; chmod a+x jre--.rpm ; ./jre--.rpm; echo "success!"' done The problem is that during the java installation I need to "read" the notice and type "yes" at the en...

How to achive single-processing mode running php scripts?

I have cron job - php script which is called one time in 5 minutes. I need to be sure that previously called php script has finished execution - do not want to mix data that's being processed. There are three approaches I used to apply: Creation of auxiliary text file which contains running-state flag. Executed script analyzes the co...

How to get the nth positional argument in bash?

How to get the nth positional argument in bash? Thanks. Edit: I forgot to say but I meant that n is a variable. ...

How do I Remove Specific Characters From File Names Using BASH

I have a lot of files that have a shared pattern in their name that I would like to remove. For example I have the files, "a_file000.tga" and "another_file000.tga". I would like to do an operation on those files that would remove the pattern "000" from their names resulting in the new names, "a_file.tga" and "another_file.tga". ...

How do you call a function defined in .bashrc from the shell?

In my .bashrc, I have a function called hello: function hello() { echo "Hello, $1!" } I want to be able to invoke hello() from the shell as follows: $ hello Lloyd And get the output: > Hello, Lloyd! What's the trick? (The real function I have in mind is more complicated, of course.) EDIT: This is REALLY caused by a syntax e...

How to make a cronjobbable script to clock uploads?

I asked a similar question over here:(http://stackoverflow.com/questions/1495604/upload-clocking-script), but then changed it and realized it's a different question now. I want to make a script called uploadtimer.sh which uploads files to various hosts and then notes the time the upload takes in a log file. Thanks to those who have hel...

Why is this grep command not coming back with anything? (Bash)

I have a text file 'samp' that I want to grep ONLY lines that start and end with uppercase vowels. Using "grep ^[AEIOU] samp" works. Using "grep [AEIOU]$ samp" works as well. But when trying to combine them to "grep ^[AEIOU]$ samp" it returns nothing. 3 things: Yes, I have lines that start and end with uppercase vowels in samp. ...

How to run a script on linux from a windows service?

Does anyone know an elegant way to initiate a bash script (to run on a linux box) from a windows service written in C#? I can only think of some combination of putty doing auto-login and automatically running a command upon login. But this seems clumsy and a bit insecure. Security doesn't need to be very high as both boxes reside on t...

A git hook for whenever I change branches?

So here's an interesting situation when using git and python, and I'm sure it happens for other situations as well. Let's say I make a git repo with a folder /foo/. In that folder I put /foo/program.py. I run program.py and program.pyc is created. I have *.pyc in the .gitignore file, so git doesn't track it. Now let's say I make anoth...

pipe, standard input and command line arguments in bash

Consider: command1 | command2 Is the output of command1 used as standard input of command2 or as command line arguments to command2? For example, cat test.sh | grep "hehe" What is its equivalent form without using pipe? I tried grep "hehe" $(cat test.sh) and it seems not correct. ...

SVN Post-Commit Script Question

We have an svn server here with multiple paths. What I'm looking for and I might not have looked hard enough is a post-commit script that whenever something is checked into a specific path, it does an ssh to another server and runs a bash script. Any ideas on a quick and easy way to do this or a post-commit script already out there? ...

awk and printf in bash

Hi, I am trying to get the rounded number of the average load in the past 5 mins. So here goes my command: uptime | awk -F, '{print $5}'|printf "%.0f\n" It seems incorrect as it always give me 0. If I tried to use a variable as intermediate between awk and printf, then it is correct avgload=$(uptime | awk -F, '{print $5}') prin...

bash return value

Hi, I like to write a script or a function (not sure which one yet) that will be called by another script. The script or function is to generate several values. How can I write these in bash so that in the other script I can get the values returned by the script or function? Examples are specially appreciated! Thanks and regards! ...

Why do processes spawned by cron end up defunct?

I have some processes showing up as <defunct> in top (and ps). I've boiled things down from the real scripts and programs. In my crontab: * * * * * /tmp/launcher.sh /tmp/tester.sh The contents of launcher.sh (which is of course marked executable): #!/bin/bash # the real script does a little argument processing here "$@" The conte...

With bash, how can I pipe standard error into another process?

It's well known how to pipe the standard ouput of a process into another processes standard input: proc1 | proc2 But what if I want to send the standard error of proc1 to proc2 and leave the standard output going to its current location? You would think bash would have a command along the lines of: proc1 2| proc2 But, alas, no. Is ...

Equivalent of select or poll in bash

Is it possible to do selects or polls on file descriptors in bash? The essence of what I'm trying to do is this: mkfifo fifo exec 3<fifo PROMPT_COMMAND="while read -t 0 line; do echo \$line; done <&3" The exec is there to keep the pipe open (otherwise it would be closed at the end of each loop). In theory, this would output anything e...

Help me make this bash script pretty, or just less ghastly

I have a simple bash script, called *print_code.sh*, that takes a file name, and prints out the file with line numbers. Here it is: FILE=$1 line=0; numLines=`wc -l $FILE | sed 's/ .*$//'` digits=`echo "l($numLines)/l(10)+1" | bc -l` digits=`echo "0$digits" | sed 's/\..*$//'` for i in `cat $FILE | sed 's/ /_=SPACE=_/g'`; do line=`echo...

Bash script for manual routes and default gateway problem

So at work i use a vpn connection and after that i have to manually set my route table so everything works, I'm trying to write a bash script which will easily distinguish between my home connection and my work connection and set the correct gateway: #!/bin/bash # Does the ppp0 interface exist? cat /proc/net/dev | grep ppp0 > /dev/null...