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"
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"
Although you may think that you have learned to master Linux command line with bash shell, there are always some new tricks to learn to make your command line skills more efficient. This article will teach you a few more basic tricks on how to make your life with the Linux command line & bash more bearable and even enjoyable.
This section will mostly deal with bash shortcuts in combination with three bash history expansion characters “!”, “^” and “#”. Bash Command History Expansion character “!” indicates start of history expansion. The “^” is a substitution character to modify a previously run command. The last optional character is “#”, which denotes the reminder of the line as a comment.
$ echo Bash Shortcuts
Bash Shortcuts
$ !!
echo Bash Shortcuts
Bash Shortcuts
!! is probably the easiest and most popular bash shortcut, which simply shows and executes your last entered command.
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/\(.\)/\1\n/g' | grep l | wc -l 2
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))
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"
:
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;
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 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!
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'
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 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'))
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 TAB
separated 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
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