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]

The below table contains a list of Python Escape sequence characters and relevant examples. You can run all below examples from python prompt.
Example:
$ python
Python 2.7.5 (default, Jun 24 2015, 00:41:19) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print u"\u041b"
Л

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!

Question

: My Python program produce a following error message upon execution:
 SyntaxError: Non-ASCII character '\xc4' in file test.py on line 1, but no encoding declared; 

Answer:

Normally the above error message is displayed by python when other characters other then ASCII are used withing your code. The solution is to either remove all non-ASCII characters or include the bellow line into your code to enable UTF-8 encoding:
# - *- coding: utf- 8 - *-

The following bash script example we show some of the way how to check for an empty or null variable using bash:
#!/bin/bash 

if [ -z "$1" ]; then
    echo "Empty Variable 1"
fi


if [ -n "$1" ]; then
    echo "Not Empty Variable 2"
fi


if [ ! "$1" ]; then
    echo "Empty Variable 3"
fi


if [ "$1" ]; then
    echo "Not Empty Variable 4"
fi


[[ -z "$1" ]] && echo "Empty Variable 5" || echo "Not empty Variable 5"

Author: Tobin Harding
Here we briefly outline some of the major use cases for brackets, parenthesis, and braces in BASH scripting, see bottom of page for definition of these three terms. Double parentheses (( )) are used for arithmetic:
((var++))
((var = 3))
for ((i = 0; i < VAL; i++))
echo $((var + 2))

Below you can find some hints on how to count an occurrence of specific character in a file or in a string. Le's say we have a string "Hello Bash":
$ STRING="Hello Bash"
$ echo $STRING
Hello Bash
Using bash shell we can now count an occurrence of any given character. For example let's count number of occurrences of a character l:
$ echo $STRING | sed -e 's/\(.\)/\n/g' | grep l | wc -l
2

Here is a simple GNU R script print a single line:
#!/usr/bin/Rscript

print("hello R")
where or execution output is:
$ ./script.R
[1] "hello R"

Let's start by a simple execution example of GNU R Rscript front-end script. Use which command to locate Rscript interpreter:
$ which Rscript
/usr/bin/Rscript
alternatively define your interpreter as /usr/bin/env Rscript
#!/usr/bin/Rscript

print("Hello R")

The total number of supplied command-line arguments is hold by a in bash's internal variable $#. Consider a following example of simple bash script which will print out a total number of supplied command-line arguments to the STDOUT:
#!/bin/bash
echo $#
Save the above into a file called eg. arguments.sh and execute:
$ bash arguments.sh 1 2 3 4
4

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

Commands history is a great feature of the bash shell. However, there are times when it is best to disable it. One good example when you might prefer your bash shell commands history to be disabled is on the production server accessible from the external network where potential attacker might gain an access to your server and re-read your history file to search for useful commands, services in use or accidentally inserted passwords. Below you can find bunch of commands to help you disable history from being stored or how to remove all currently stored commands.

Where are history commands stored

All commands your enter on the shell are stored within your local directory into a file called .bash_history. This is a default history file defined by HISTFILE variable:
# echo $HISTFILE
/root/.bash_history

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

Below is a simple function to check for a prime number. The function is_prime_number() returns False if the number supplied is less than 2 and if the number is equally divisible with some other number different than 1 and itself. If none of the previous conditions apply the function will return True. The below python script will let user to decide how many numbers needs to be check to see whether the number is prime number:
#!/usr/bin/env python

prime_numbers = 0

def is_prime_number(x):
    if x >= 2:
        for y in range(2,x):
            if not ( x % y ):
                return False
    else:
	return False
    return True
	        

for i in range(int(raw_input("How many numbers you wish to check: "))):
    if is_prime_number(i):
        prime_numbers += 1
        print i

print "We found " + str(prime_numbers) + " prime numbers."

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

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in: