Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Tuesday, August 2, 2011

ffmpeg: Creating A Single Image Video With Audio via CLI

Here's a nice quickie for you command line junkies. So say you wanted to share an audio file, but for sites like Facebook or Google+ you only have an option to upload a video. Okay sure, just create a video with the audio embedded.

My friend left me a hilarious voicemail a while back that I wanted to share with friends. I also wanted to set an image to constantly display just for viewing pleasure. So, bake the image to loop add in the mp3 voicemail and voila!


ffmpeg -loop_input -i Downloads/P1010155.png -i Downloads/30ad00fa24c1dcdf7479271f290c82252216718a.mp3 -shortest tmp/output.mp4

Friday, January 14, 2011

No Screen?! Ok, Run Processes In The Background

I would hate for this post to become a rant (even though it kind of is), so I will just call this a "quick tip".

A Security Issue, But A New Headache For Screen Users

So, we implemented this new policy in our network where we cannot store our ssh keys on any server, they must be stored on your local computer only. Okay, that's fine, except for the fact that I like to take my laptop home and occasionally I need to run scripts that take a really long time to finish.

I used to be able to use this one server as a "gateway" to all of the other servers in our network. Luckily, screen is installed and I can just have a screen session open, run a process or two on any server, and then detach from the session, shutdown my laptop, go home, and check on it later.

But now with my laptop being the "gateway" I would either need to stay and wait for these processes to finish or risk aborting them when I log out.

nohup To the Rescue

Just a reminder, if you can't screen, there is always nohup and sending that process to the background!

Example: nohup some_process_or_script.sh &

I won't get into details, but here are some related/helpful commands to use along with this:
jobs, bg, fg

Check out their man pages and I'm sure you'll be happy that you can still be able to run processes before leaving the office and then check back on them later!

Cheers!

Thursday, August 27, 2009

Quick Bash One-Liner

Well, I'm here trying to wrap up things for the day and just found a very useful technique to get a directory without the filename from the listing if using find.

So, I'm sure you were all thinking, "well why don't you just use `find` with `-type d`?" Well, for this specific task I will be using `find` to get a list of all files and then I need whatever that directory is for another script. So booyah, in your face! (hehe jk)

So, apparently if I have a directory structure string with a file at the end of it I can drop the end part like so:

f1=/mnt/www/tmpdrin/dir1/dir2/fileindir.txt
f2=${f1%/*};
## $f2 will now be /mnt/www/tmpdrin/dir1/dir2


Sweet as!


And if you were curious how I was using it as a one-liner, here ya go:

for f in `find /mnt/www/tmpdrin/ -type f`; do f2=`echo "$f" | sed -e 's!/mnt/!!g'`; f3=${f2%/*}; echo "$f3"; done


Obviously, I'm doing a lot more here than just echoing, but this is just an example mmmkay? ;)

Cheers!

Post Script: It's been a while for posts, but I've just been working on proprietary stuff lately and nothing would be too useful for ya'll. Sorry!

Friday, March 13, 2009

Command Line Upgrade Wordpress Databases

Oh yay, we're going to upgrade WordPress here at work! Now we have a couple hundred WordPress databases at work, so I don't want to have to manually load up a web browser and click the "WordPress Upgrade" button a hundred or so times. In technology, if you have to do the same process over and over, you should always look for a way to automate it, and that is what I've done here.

The PHP script is rather simple. If you look at it, it basically defines which database, db user, db password, db server, etc. and then it runs WordPress' wp_upgrade() function. That simple! Honestly, the hardest part (for me at least) was figuring which WordPress files I needed to include and what order they go in! (In the beginning, I traversed the order of my require statements. Doh!)



\<\?php //this gets hosed by blogger so i'm trying to "escape" it

if ($argc != 2) {
print "Usage: php $argv[0] [wpdb]\n";
exit;
}
$db = $argv[1];
define( 'ABSPATH', dirname(__FILE__) . '/' );
define('WP_ADMIN', true);
define('DB_NAME', $db);
define('DB_USER', 'wpuser'); // Your MySQL username
define('DB_PASSWORD', 'wppass'); // ...and password
define('DB_HOST', 'mysql.server.com'); // 99% chance you won't need to change this value
require('wp-load.php');
require('wp-admin/includes/upgrade.php');
print "Upgrading " . DB_NAME . "...\n";
wp_upgrade();
print "\n\nUpgrade for " . DB_NAME . " complete!\n";
?>


Now as you can see, I set up my PHP script to handle the database name as an argument. So now, all I need to do is write a bash wrapper script! I haven't done it yet, but it'll basically be similar to this:


for db in `cat listofdbs.txt`; do php ajc-wpupgrade.php $db; done;


And Bob's your uncle!

Friday, October 24, 2008

Using a Script to Edit a conf File

Well, because I have been uber busy here at work, I haven't been able to write any blog posts. But, since I've been super productive at work, I get to write up a few posts about some more tools I've written to add to my arsenal. w00t!

So my latest task involved editing some of our web server's Apache confs. It consisted of adding a new alias so that we can call in some new app someone else wrote. Okay cool. Alias? Sure not a problem. Er...wait! The alias requires the site's directory name as part of the path! Oh no! That means I can't create one global alias!

Ah, see that's when I thought of writing this script that will look for a specific line in the vhost entries and will enter the new line after it. Pretty nifty eh? :)

So basically, what we have here is a list of all sites in a text file. Now we can use the site's directory name read in as a variable. It will then use awk to search for a specific line, and then print the new line you need right below it!

So if you take a look at the code, I want to add this line:
"Alias /newalias /mnt/www/$site" (where $site is the site directory name variable)
right after each line that fits the regex that searches for any line that has "Alias" and "reports" in one line followed by the site dir name at the end of the line. Obviously if you want to utilize this script you should edit the regex to your needs.

And that's it! Now think of all of the time saved rather than adding a new line manually to EACH vhost entry on EACH server! whoa....scary...


#!/bin/bash

exec < sites.txt
while read site ; do
echo "processing $site..."
awk '{print $0} '"/Alias.+reports.+\/$site"$/'{print " Alias /newalias /mnt/www/'"$site"'"}' httpd.conf > newhttpd.conf && mv newhttpd.conf httpd.conf
echo "done!"
done

Thursday, October 2, 2008

Shell Scripting to Read Data From a File

Okay, so here's your problem: you have a file that contains data that you'd like to parse through a script. It's a lot of data and you don't want to sit there and continuously do the same thing over and over!

The data can be set up as one long column of data or multiple columns of data. In this case for simplicity I'll only use one column of data.

So let's say your data file is as follows:

dog
cat
cow
...
...
(long list of data)


These represent parts of filenames that you need to clear out. You don't want to sit there typing cat /dev/null > dog
cat /dev/null > cat
...
So you come up with a master plan to script the process. The script will be short, sweet, and absolutely time saving!

You first need to read in the file; so this line does the work for you: exec < files.txt. Obviously, in this example my "files.txt" is the filename that contains the data I want to read. After you've opened up the file for reading, you can now parse through the file with a while loop:
while read file ; do
[do some work here]
done


Now note, "file" is the variable that we're assigning to the column. You can call the variable whatever you want. Also remember that I am being simple with this example and only have one column of data. If I wanted to do multiple columns of data, I would just assign more variables to each column. e.g. while read col01 col02 col03 ; do where $col01, $col02, and $col03 will be my new variables.

That's it! I'll leave the customization to you. Cheers!

Here is the full script example:
#!/usr/bin/bash

exec < files.txt

while read file ; do
if [ -s /mnt/$file ] ; then
cat /dev/null > /mnt/www/vdir/z/ec/$file
else
echo "/mnt/$file already empty!"
fi
done