Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Wednesday, February 4, 2015

Text editor (command line)

Raspberry pi text editor (command line)

Note: This is a translation of the Spanish version of my blog article I posted some years back

Before even mastering the command line, you will need to learn a way to edit text files from the command line. Be it to make configuration changes, or write a script (sequence of commands), you'll be a lot more productive if you learn how to use one.

Raspbian comes equipped with nano (easy to use when starting out) and vi (the standard - found on all *nix servers and desktops), but, there is something even better: vim (VI iMproved)

If you've never used vi before, might want to start with nano instead, or read a vi tutorial or even better, an interactive vim tutorial (highly recommended!!), and cheat sheet)

Normally, on Linux and modern Unix, vi is really vim, but on Raspbian, vi is vi. Let's change this:
fdion@raspberrypi ~ $ sudo apt-get install vim
There are a few things to add to .vimrc to make it better (in /home/user):

syntax on
filetype indent plugin on
set modeline

fdion@raspberrypi ~ $ pwd
/home/fdion
fdion@raspberrypi ~ $ vi .vimrc
(type i for insert, type the above 3 lines, hit Escape and :wq)
fdion@raspberrypi ~ $ ls .vimrc
.vimrc
Excellent, now scripts and code (like Python) will look much better, easier to read in color:


fdion@raspberrypi ~ $ vi file.py 


Image


There are a few lines you will want to use specifically for Python scripts:

Image

The first line is always the "shebang" line:

#!/usr/bin/env python

Here, we tell the operating system shell executor to pass the script to the python interpreter. For other interpreters, just replace python with whichever interpreter will handle the script.

Another thing for a python script that I like to use, a docstring (description) for the script, between """ and """. 

The seventh line above (in the code example) is for vim itself for proper tabs as 4 spaces (you want that for aligning your Python code, and is a nice to have in general for shell scripting):

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4


If you are dealing with utf-8 code (for example accents or special characters) then you need to define the following:

# vim: set fileencoding=utf-8


Francois
@f_dion

Tuesday, October 8, 2013

$1 in Python

Interesting question


"Why cant I use $1 in a Python script to get the first argument to a script? What is the easiest way to do something like that in Python?"

$1 in a shell script gives us the first argument to the script, BTW.

I get a lot of questions through email, and in person. This one was part of an interesting exchange. The reasoning went like this, since the shell is processing the script, I should be able to use $1.

The problem is that it doesn't work like that. Dennis Ritchie introduced the #! (pound bang or shebang line) concept at Bell Labs and announced it publicly in 1980. In the exece function of sys1.c could be found the following:

 else if (u.u_exdata.S[0]=='#' && u.u_exdata.S[1]=='!' && indir==0) {
  cp = &u.u_exdata.S[2];
  while (*cp==' ' && cp<&u.u_exdata.S[SHSIZ])
   cp++;
  u.u_dirp = cp;
  while (cp < &u.u_exdata.S[SHSIZ-1]  && *cp != '\n')
   cp++;
  *cp = '\0';
  indir++;
  iput(ip);
  ip = namei(schar, 0);
  if (ip==NULL)
   return;
  goto again;
 
 
So, once it was established that a script started with #! and a variable number of spaces, what followed was the path to an executable that would process this script. No search, so it has to be a full path.

In that regard, if the line starts with #!/usr/bin/python or #!/usr/bin/env python, the shell is out of the loop and no $1. Python will process the script. There is an sh module that makes it appear like we are mixing Python and shell in one script, but it is still all Python code.

Simplest shell script


#!/usr/bin/bash

echo $1

Simplest Python


#!/usr/bin/env python
from sys import argv as s


print(s[1])

Fairly similar except we had to specify an import statement, and I aliased argv to s so it looked like $ in the shell script.

The "problem" (or difference) with the Python script is that it will error out if no arguments are given when run:

$ ./arg.py
Traceback (most recent call last):
  File "./arg.py", line 5, in <module>
    print(s[1])
IndexError: list index out of range

A closer equivalent



#!/usr/bin/env python
from sys import argv as s

print(s[1] if len(s)>1 else '')

Here we are simply testing to see how many items are in the argv list. The first item is the script name, so if we have >1 items, we have at a minimum one argument, so it is safe to get [1]. If not, we will return an empty string, just like a shell script would do. A try / except IndexError would have also worked in this context.

See also

I would also investigate argparse, opster and docopt. argv is really bare metal and there is no point in reinventing the wheel.


François
@f_dion