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

Below you can find a short script on how to convert a binary number to decimal using python casting. Casting is when you convert variable from one type to another. In this case we use python casting to convert string to decimal number that is integer. Therefore, when you attempt to use casting to convert variable type to integer make sure that you variable type imput is a string. Create a following file with a following content:
#!/usr/bin/env python

while True:
    try:
        # Try to convert bunary to decimal
        decimal_num = int(raw_input("Enter a binary number: "), 2)
        # If we fail we ask again user to enter binary number
    except ValueError:
        print "Your input is not a binary number! Please try again."
    else:
        # Exit program if the conversion from binary to decimal was successful
        break
# print converted decimal number
print decimal_num
Save your script as eg. binary2decimal.py, make it executable and execute:
$ chmod +x binary2decimal.py
$ ./binary2decimal.py

Here a listed few of many ways how to extract number from a string. For all the examples below we will use sentence I am 999 years old. where the aim is to exctract nunber 999. Let's start by using tr command:
$ NUMBER=$(echo "I am 999 years old." | tr -dc '0-9')
$ echo $NUMBER
999

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'

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: