How to obtain an user input with Python raw_input function example

The Python raw_input() function is used to read a string from standard input such as keyboard. This way a programmer is able to include user inserted data into a program. Let’s start with a simple example using python script to ask for an user name.

print "What is your name?"
name = raw_input()
print "Hello %s!" % name

First, we print string What is your name? telling the user what we expect him to input. Next, using the raw_input() function the standard input is assigned to a variable name. Lastly, we print the value of variable name to standard output.

$ python input.py 
What is your name?
Monty Python
Hello Monty Python!

Read more

Check your GMAIL inbox for new emails with Bash script

In case you wish to automate your things with your gmail email. Here is a simple script on how to access your gmail account with bash script. Before you run the script make sure that curl command is available on your system as this script depends on it. The below script is a great way to quickly check your gmail inbox with a single command. Open your favorite text edit and create a bash script file with some arbitrary file name eg. check_email.sh

#!/bin/bash

username="USERNAME"
password="PASSWORD"
echo
curl -u $username:$password --silent "https://mail.google.com/mail/feed/atom" |  grep -oPm1 "(?<=<title>)[^<]+" | sed '1d'

Read more

How to access and print command line arguments with Python

The following is an example on how to pass and access command line arguments which a Python script. Save the following python script to a file eg. python-arguments.py

from sys import argv

name, first, second, third, fourth = argv

print "Script name is:", name
print "Your first argument is:", first
print "Your second argument is:", second
print "Your third argument is:", third
print "Your fourth argument is:", fourth


# Alternatively we can access "argv" argument list directly using range. For exmaple:

# Print all arguments except script name
print argv[1:]

# Print second argument
print argv[2]

# Print second and third argument
print argv[2:4]

# Print last argument
print argv[-1]

Read more

Extract all URLs using Beautiful Soup and Python3

The following link will extract all URL’s for a given web page.

#!/usr/bin/env python3

# Python Version:  3.4.2
# bs4 version: 4.3.2-2

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://gnu.org") # Insert your URL to extract
bsObj = BeautifulSoup(html.read());

for link in bsObj.find_all('a'):
    print(link.get('href'))

Read more

How to fill all empty valued cells within a CSV file with sed and bash shell

The following bash script can be used to fill empty cells within a CSV file. We assume that your file is evenly distributed so that it contains same number of columns in each row separated by comma. If your file is TABseparated use a following linux command to convert it to comma separated value file before you proceed.
Example:

$ cat test 
1       2       4       4
2       3       3       3
$ sed 's/\t/,/g' test 
1,2,4,4
2,3,3,3
OR
$ cat test | tr '\t' ','
1,2,4,4
2,3,3,3

Read more

Writing a C style bash for loop – example

If you are stubborn C programmer and wish to get your way when using BASH you will be happy to know that BASH offers C style syntax for writing for loops. Below you can find two examples of C style bash for loop:

Simple c-style bash for look with three iterations:

#!/bin/bash
MAX=3
for ((i=1; i <= MAX ; i++)) ; do
	echo "$i"
done

Read more

-bash: /bin/rm: Argument list too long – Solution

Symptoms

This error message appears when you try to remove, move or copy a long list of files. When using your shell a command can only accept a limited number of arguments. When the number of arguments supplied to the command exceeds the permitted number of arguments an error message will appear:

-bash: /bin/rm: Argument list too long

linux command to find your limit for maximum arguments:

# getconf ARG_MAX
2097152

Example:

# rm *
-bash: /bin/rm: Argument list too long

Read more

How to store all shell commands immediately after execution into .bash_history file

When using a bash shell all your entered commands are remembered by history library. The history library will keep track of every command you have entered. This is a default for most if not all Linux systems. However, the commands you enter are first temporarily stored into an internal memory and are written to your ~/.bash_history  only after you properly exit your shell session.

Depending on your shell usage this may cause some undesired results. For example, if your connection to a remote host gets disconnected, your history file will not get updated and thus you will lose all you previously entered commands. Furthermore, while your commands for one session are temporarily stored within system’s internal memory you would not be able to access it from another shell session.

Use the following linux command to force your shell to append every command entered during a current shell session into ~/.bash_history file:

shell 1: $ history -a

Read more

C programming tutorial

This series of articles is dedicated to development on Linux systems.

This tutorial focuses on C programming and covers such concepts as types, operators and variables, flow control, functions, pointers and arrays, structures, basic I/O, coding style and building a program as well as packaging for Debian and Fedora or getting a package in the official Debian repository.

Read more