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

Saturday, October 18, 2014

Node.js Version Manager (nvm) Alternative Node.js installation on gnu/Linux

The Node.js packages distributed by gnu/Linux distributions tends to be out of date. If you looking for an alternative method for installing Node.js on gnu/Linux. Then Node Version Manager (nvm) is a good choice. This simple bash script lets you install and manage multiple active node.js versions.

You can download Node Version Manager(nvm) from the projects github page. Digital Ocean community site has a good getting started tutorial for Node Version Manager (nvm).

Installing Node.js Version Manager (nvm)


# Download and the installation script
$ curl https://raw.githubusercontent.com/creationix/nvm/v0.17.2/install.sh | bash

# Activate nvm (You might want to add this line to your .bashrc or .profile file)

$ source ~/.nvm/nvm.sh 

# Discover other installable versions of Node.js 
$ nvm ls-remote 

# Install the stable version of Node.js 
$ nvm install v0.10.31

# Install another version of Node.js
$ nvm install v0.11.13

# Check the installed versions of Node.js 

$ nvm ls 
->  v0.10.31
    v0.11.13
      system

# Use v0.10.32 version of npm 
$ nvm use v0.10.31 

# Run using particular version Node.js 

$ nvm run 0.11.13 

# Alternatively use Node.js installed by your package manager 
$ nvm use system 

Thursday, August 5, 2010

How to resize photos with Bash shell script

Ivo Kaspar wrote this guide to resize multiple images using simple Bash shell script. It uses the convert program from ImageMagick, the swiss-army knife of command line image processing software. The original guide is edited and rewritten in some places.

You'll find this guide interesting if you want to resize a lot of pictures to upload to your website or email them. First you need to install the 'imagemagick' package if it is not installed on your computer. To install imagemagick, copy this line into your terminal application and confirm with your password. Close the terminal after it finished the installation. sudo apt-get install imagemagick

Open the text editor and paste these lines into it:


#!/bin/bash
for i in *.JPG; do convert $i -resize 10% $(basename $i .JPG).jpg; done

Save the document and give it a sensible name to recognize it later on.

Note that this script will work only with files that has uppercase '.JPG' extension and renames them to lower case '.jpg'. Some digital cameras follow this kind of file naming schema. You may like to change this to suit your needs.According to its manual page ImageMagick can read, convert and write images in a variety of formats (about 100) including GIF, JPEG, JPEG-2000, PNG, PDF, PhotoCD, TIFF and DPX.

If you want to define the size of the picture replace the "10%" with the amount of pixels you want it to have, 400x600 is a usable resolution. Of course it is also possible to keep the relative scale with any value instead of the “10” followed by "%" postfix. To change the file format to something other than “.JPG” you have to change the last “.JPG” into the format of your choice.

If you want to rename the pictures, you have to add something behind the "(basename $i .JPG)" and the ".JPG". I usually add an "­-s" (for small) to indicate the reduced size of the pictures.

When you run the script it will proceed to resize all the pictures in the current folder, but not in any sub folders. Keep in mind that:

  • Small pictures are small forever, you can't revert them.
  • The filenames must not contain any space
  • The filename must match to the first two “.JPG” or “.jpg”

Examples Scripts:


# capital .JPG files, 10% size, no rename, it will overwrite the old
pictures
#!/bin/bash
for i in *.JPG; do convert $i -resize 10% $(basename $i .JPG).JPG; done
---
# lower case .jpg files, 10% size, renamed to capital .JPG (with ubuntu it's
a different file, not with windows!)
#!/bin/bash
for i in *.jpg; do convert $i -resize 10% $(basename $i .jpg ).JPG; done
---
# capital .JPG files, 10% size, adds an -s in the name of the new ones,
keeps the old ones
#!/bin/bash
for i in *.JPG; do convert $i -resize 10% $(basename $i .JPG)-s.JPG; done
---
# capital .JPG files, 600x400 size, no rename, it will overwrite the old
pictures
#!/bin/bash
for i in *.JPG; do convert $i -resize 600x400 $(basename $i .JPG).JPG; done

Wednesday, November 12, 2008

Bash Path Security Gotcha!

The devil they say is in details, even the simple Bash path variable can open some security problems on your box. Every Bash documentation out there warns us not leave '.' in the path (it matches current directory). They however don't seems to mention that leaving a dangling ':' (colon) at the beginning or the end of Bash $PATH does create the same security vulnerability.

# Standard Bash $PATH $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games # Let's get evil, append a colon at the end $ export PATH="$PATH:" # Let's check the tainted Bash $PATH $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games: # Create Trojan application (of sorts) $ echo "echo foo"> app $ chmod +x app # Run the command and Viola! $ app foo

Tuesday, October 30, 2007

Running automated package updates with Cron

LinuxLove.org's recent post about automatically running apt-get upgrades daily with cron using a simple bash script can be easily done using Cron-Apt package itself.

Please do note that such automated package upgrades in apt based distro's is a very bad idea. Joe had rewritten a detailed account about the problems and security issues involved in Debian-Administration.org few years back.

By default, cron-apt will only download updates -- it will not install them. I know other packages for other distributions like up2date will automatically install the updates for you, but I've learned to like this and that in the long run, automatically installing updates is a Bad Thing. Why? Well when I was experimenting with auto-installs, I ran into problems:

  • Even though I told apt to say Yes to everything, it still prompted me for input on config file conflicts (was I doing something wrong?). This caused the script to hang indefinitely in the background until it was killed.
  • Auto-accepting configuration changes can ruin your day. With Debian, it's fairly easy to back out of a change and hopefully you have a backup copy of the config file, but that's not the point. Good System Administration means you should never be in that position in the first place. "OK, what if I just deny all configuration changes and leave my copy?", you ask. Well, what if the upgraded program uses new syntax in its config file. Your program is now broke and you're in the exact opposite position as you were with auto-accepting.
  • Just as you should always review configuration file changes, always review what packages are being installed. If you know that installing that new version of Samba will mean some extra work in other places, don't install it yet.

Bottom line: Yes, it can be very tedious to manually review each update batch -- especially if you have several servers -- but that is part of your job when you are running a server. Deal with it!

cowsay: a configurable talking and thinking cow

Today's Debian Package of the day Cowsay reminds me of a dull day few years back when I wrote this 'cowwords' script to amuse myself.Even a useless tool like cowsay can be interesting with creative use, CrunchBang has another cool hack using vrms and cowssay Happy Hacking! :) .

Cowsay is a useless but very fun text filter written in Perl. If you send some text into cowsay, you get an ASCII cow saying your text. For example, cowsay Hello, World! prints


_______________
< Hello, World! >
 ---------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


Script in public domain originally posted on FSUG-Bangalore Mailing-list

#!/bin/sh # # SCRIPT: cow-words.sh # AUTHOR: Rakesh 'arky' Ambati (http://www.linuxgazette.com/blog/4864) # DATE: 17 January 2005 # VERSION: you_must_be_kidding # # CREDITS: I Thank Karma_Police for the 'cow file randomizer code' # # PURPOSE: Retrieves the Dictionary.com 'Word of the Day" RSS Feed and # a random character teaches them. # # DEPENDS: 'xmlstarlet' # 'cowsay' # Both are available packages in Debian (Sid) repository. # To install them just type 'apt-get install xmlstarlet cowsay' # # # set -x # Uncomment this for debugging. # # ##################################### # Use 'cowsay -l' to the full list of cow files in your system # # (Sorry fellas I removed possible offensive 'sodomized*, head-in,telebears, # # kiss' cow files # ###################################### cows=(apt beavis.zen bong bud-frogs bunny cheese cower daemon default dragon dragon-and-cow elephant elephant-in-snake eyes flaming-sheep ghostbusters hellokitty kitty koala kosh luke-koala mech-and-cow meow milk moofasa moose mutilated ren satanic sheep skeleton small stegosaurus stimpy supermilker surgery three-eyes turkey turtle tux udder vader vader-koala www) random=RANDOM%44 ######################################### ## Get the XML feed and do the selection ## ######################################## /usr/bin/xmlstarlet sel --net -t -m "/rss/channel/item/description" -v "." "http://dictionary.reference.com/wordoftheday/wotd.rss" | /usr/bin/cowsay -n -f ${cows[random]} # End of the script

Thursday, October 25, 2007

Bittorrent Mainline Port Randomizer / Custom Settings script

Here is a quick and dirty bash script that I used when Bittorrent Mainline removed the random port option.

  • The script uses a random port to listen from a given range
  • Sets custom upload limits,refresh intervals and client address(IP)
  • Disables Linux uPnP
  • Changes the default data directory (default is ~/Desktop/Bittorrent)
  • Saves the downloaded file to current directory

Copy the code to text file and save it as Bitty.sh and set execute permission with 'chmod a+x Bitty.sh' and launch with ./Bitty.sh <torrentfile>.torrent

You may need to change the values to suit your needs. Also note that the You the /usr/bin/bittorrent-console is commandline client from the bittorrent Mainline for GNU/Linux and change the location on your Linux distro.


#!/bin/sh
#
#
#set -n
# Bitty.sh - a port randomizer for bittorrent mainline
#set -x 
#
MAXPORT=25000
MINPORT=20000

number=0

while [ "$number" -le $MINPORT ]

do 
  number=$RANDOM
  let "number %= $MAXPORT" 
done 


exec /usr/bin/bittorrent-console  --ip debbox.no-ip.org  --display_interval 30 
--no_upnp --minport $number  --maxport 25000 --max_upload_rate 5120 
--max_initiate 70  --save_incomplete_in /mnt/BigMaMa/.bittorrent/incomplete
 --data_dir /mnt/bigMaMa/.bittorrent  --save_in . "$1"


Please feel free to improve the script !

Popular Posts