LAMP How To – Open Source At Work

Only Passion Matters

Entries Tagged ‘bash’

bash: copying files without prompt

It can be annoying that the ‘cp’ command keep prompting if you are to overwrite some files. This is due the cp alias. To fix that,

unalias cp
cp -af source target

done.

Share and Enjoy:

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz

Bash: Simple command to mass replace content in directory

Imagine that we need to mass change the content in certain directory. A combination of find and sed can save the day. It is very to do it in perl as well.

find /your/dir -type f | xargs sed -i ’s/old/new/g’

Share and Enjoy:

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz

bash expect: script to handle command line prompts

expect is very useful to automate login process. I thought it is helpful when doing testing as well. A simple login script might work like this:

#!/usr/bin/env expect
eval spawn “/path/program”
expect “^Enter Auth Username:”
send “user\n”
expect “Enter Auth Password:”
send “password\n”

To install expect, “yum install expect”

Share and Enjoy:

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz

bash: testing conditions using if-grep

this expr can be really useful to determine if a term has been found or not using grep. short and sweet enough for me.
if [ ! -z $(echo $var | grep "cond") ]; then echo “found”; else echo ” not found”; fi

Share and Enjoy:

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz

bash: passing output from one program to another

sometimes you want to be able to pass output from one program to another, say from bash to php. There is a neat trick to do it. In php, we execute the bash script restartapache.
<?php
$command=”/usr/local/bin/restartapache {$_GET['server']}”;
exec($command, $output); foreach ($output as $v)
{ echo “$v <br/>”; }
?>
then in the bash, we write the output to a [...]

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz