Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Friday, 11 May 2012

Connecting to a VPN from the command line in Linux Mint / Ubuntu

Set up the VPN using the Network Manager applet, then open a Terminal. The command to connect to a VPN is:
 nmcli con up uuid <your-uuid-here> 

To find out the UUID of the VPN connection, use:
 nmcli -p con list

The command to disconnect from a VPN is:
 nmcli con down uuid <your-uuid-here>

How to run a script requiring sudo without needing a password

To run a script that has a sudo command in it, without needing to enter a password whenever the script is executed, add an entry in sudoers.
First, open sudoers using visudo:
 sudo visudo  

Then add the following line to the bottom of the file:
 <user> ALL=NOPASSWD: <absolute_path_to_script_file>  

Where <user> is the user running the script, and <absolute_path_to_script_file> is...self explanatory :). Though I've found that the script needs to be placed in /usr/bin/ to for the entry in sudoers to be effective.

Save the changes to the file, and log in and out to enact the change. Now, when you sudo script, there will be no prompt for a password.

Monday, 23 April 2012

Creating a Redcar Launcher in Linux

The inspiration for the Redcar editors name

To create a Redcar launcher in Linux, first create a script which launches Redcar from the command line (I keep my scripts in ~/bin). Save this as start_redcar.sh, or similar (the .sh extension is required). The contents of this script are:
 #!/bin/bash
 source ~/.bashrc  
 /home/<USERNAME>/.rvm/gems/ruby-1.9.3-p125/bin/redcar  
Redcar Launcher Script

In the above script, <USERNAME> is my Linux username, and it's followed by my Redcar installation path. Your Redcar installation path may be different - to find it, open a terminal window and type:
 which redcar  

This will output your Redcar installation path.

The key to the above script is the source command. This means the launcher will have access to your RVM and Redcar installations - without this line, the script will work when you run it from within a terminal window, but fail when you create the launcher, as the terminal has access to .bashrc, whereas by default, the launcher won't.

To test the script, first make it executable, by opening a terminal window and navigating to the script directory. Then run
 chmod +x start_redcar.sh  

Then launch the script, typing
 ./ start_redcar.sh  

Where start_redcar.sh is the name of your script.

If Redcar opens, everything has gone according to plan so far - now we need to create the launcher. To do this, right click on the desktop, and find Create New Launcher. Under Name, enter "Redcar" (without the quotes), and under Command, browse to the script we previously created. Finally, to give the launcher the Redcar icon, click the icon, and browse to:
 /home/<USERNAME>/.rvm/gems/ruby-1.9.3-p125/gems/redcar-icons-0.3/icons/redcar-icon-beta.png  

Press OK and the launcher will be created on your desktop. To add this to Cairo-dock, or another Linux dock interface, simply drag the launcher onto the dock.

Examples of the two files created above can be found in this gist. Enjoy your new Redcar launcher!

P.S!  Are you learning Ruby or Ruby on Rails? I highly recommend The Ruby Programming Language and The Rails 3 Way as useful references. Of course, you can always check out the bookstore for other books I've found useful.

Update May-20-12: This post is now linked to from the Redcar Wiki!


Tuesday, 3 April 2012

Searching Flickr using the Flickraw gem

The more I look at Ruby, the more I appreciate just how powerful and succinct  it is. I'm sure my current Ruby coding style isn't yet close to The Ruby Way,but I'll keep working on that. In the meantime, I wrote this script over lunch, in preparation for a Rails application I have in mind. The script searches flickr for photos tagged with a word you supply as a command line argument, then picks a random one and launches the browser to display it. You'll need both the Flickraw and Launchy gem installed for this script to run. You'll also need a flickr API key, which you can apply for here.


Install required gems:
 gem install flickraw  
 gem install launchy  
The code:
 #!/usr/bin/env ruby  
 require 'flickraw'  
 require 'launchy'  
 FlickRaw.api_key    = 'YOUR_API_KEY_HERE'  
 FlickRaw.shared_secret = 'YOUR_SHARED_SECRET_HERE'  
 results = flickr.photos.search(:tags => ARGV[0])  
 rand = Random.new.rand(results.length)  
 info = flickr.photos.getInfo(:photo_id => results[rand].id)  
 url = FlickRaw.url_b(info)  
 Launchy.open(url)  
 (replace YOUR_API_KEY_HERE and  YOUR_SHARED_SECRET_HERE with your information from flickr)


Save this as flickr_single_tag.rb, and then run it, supplying a single search tag on the command line:
 ruby flickr_single_tag.rb surf  


Last time I ran it (with surf, as above), I got the image below. It's kind of fun to see what kind of images the returns! 





This gist contains the code from this post. 

Friday, 30 March 2012

String Interpolation in Ruby

To display the contents of a variable using puts, use the syntax
#{variable_to_display}

For example, to if x=5, to display "The value of X is 5", do: 
x=5
puts "The value of X is #{x}"  

To test this, save the above two lines to a file called string_interp.rb  and then do
 ruby string_interp.rb  


Saturday, 25 February 2012

Changing the primary monitor in Linux Mint (and other distributions)


*** Update: To persist this change between machine restarts, see this post ***


1) Save this script as monitor-switcher.sh:

 #!/bin/bash  
 # Author: Andrew Martin  
 # Credit: http://ubuntuforums.org/showthread.php?t=1309247  
 echo "Enter the primary display from the following:"               # prompt for the display  
 xrandr --prop | grep "[^dis]connected" | cut --delimiter=" " -f1     # query connected monitors  
 read choice                                        # read the users's choice of monitor  
 xrandr --output $choice --primary                         # set the primary monitor  

2) Make executable with:
 chmod +x monitor-switcher.sh  

3) Execute by typing:

 ./monitor-switcher.sh  


4) Enter the name of the monitor you want to be the primary monitor.


All credit to the article here.