Search This Blog

echo with color in shell script

  • The colour codes:
    Black        0;30     Dark Gray     1;30
    Blue         0;34     Light Blue    1;34
    Green        0;32     Light Green   1;32
    Cyan         0;36     Light Cyan    1;36
    Red          0;31     Light Red     1;31
    Purple       0;35     Light Purple  1;35
    Brown/Orange 0;33     Yellow        1;33
    Light Gray   0;37     White         1;37
  • Example:
    RED='\033[0;31m'
    NO_COLOR='\033[0m'
    echo -e "${RED}Hello World${NO_COLOR}"
    

See also

grep to exclude lines with specific strings

grep with -v (invert) option will exclude the match lines in the output:
cat country.txt | grep -v "United States"

Tabs in bash script

echo with -e option will enable special characters:
echo -e "Name:\tJhon"

Python: check if file or directory exists

  • os.path.exists to check if file or directory exists.
    import os
    os.path.exists('/tmp/1.txt')
    os.path.exists('/tmp')
  • os.path.isfile to check if file exists.
    import os
    os.path.isfile('/tmp/1.txt')

Python: Get file absolute path

Use os.path.abspath function:
import os
os.path.abspath('abc/abc.txt')

Clear history of QuickTime Player on Mac OS X

  • To clear recent items, File>Open Recent>Clear Menu
  • To prevent the history from being created, run the following commands in Terminal:
    defaults write com.apple.QuickTimePlayerX NSRecentDocumentsLimit 0
    defaults delete com.apple.QuickTimePlayerX.LSSharedFileList RecentDocuments
    defaults write com.apple.QuickTimePlayerX.LSSharedFileList RecentDocuments -dict-add MaxAmount 0
    

Clear history of MplayerX for Mac

Open Terminal and type the following command:
defaults delete org.niltsh.MPlayerX.LSSharedFileList RecentDocuments
To prevent history from being created:
defaults write org.niltsh.MPlayerX NSRecentDocumentsLimit 0

Python: get the index of a char in a string

find and index method are available. The difference is that find returns -1 when what you're looking for isn't found, index throws an exception.
s='abc'
print(s.index('b')) # 1
print(s.find('b')) # 1
print(s.find('d')) # -1
print(s.index('d')) # exception

Python: string types in 2.x and 3.x

  • In Python 2.x, basestring is the base class for both str and unicode, while types.StringType is str. If you want to check if something is a string, use basestring. If you want to check if something is a ascii (bytestring), use str and forget about types.
  • Since Python 3.x, types not longer has StringType; str is always unicode. basestring no longer exists.

python: return inside with block

if returns value inside with statement, will the file be closed eventually?
The answer is YES

with open('myfile.txt') as f:
return [line for line in f if len(line)>80]

Ubuntu: system wide proxy settings

  • edit /etc/bash.bashrc
    export http_proxy=http://192.168.1.100:8000/
    export https_proxy=http://192.168.1.100:8000/
    export ftp_proxy=http://192.168.1.100:8000/
    export no_proxy=localhost,127.0.0.0/8,192.168.0.0/16,*.somedomain.org,172.23.65.*

python: iterables and iterators

A class with __iter__() method or __getitem__() method is iterable. The __iter__() method returns an iterator. A iterator object has a next() method (or __next__() method in python 3.0).

class Iterator(object):
def next(self):
pass

class Iterable(object):
def __iter__(self):
return Iterator()

see also

python: len() function and __len__() method

len is a function to get the length of a collection. It works by calling an object's __len__ method.

class AClass(object):
def __init__(self):
self._list = [1,2,3]

def __len__(self):
return len(self._list)

a=AClass()
print(a.__len__()) # 3
print(len(a)) # 3

python: call another static method


class AClass(object):
def __init__(self):
pass

@staticmethod
def a():
AClass.b()

@staticmethod
def b():
pass

see also

Install thinkfan fan control utility on Lenovo thinkpad laptops

I.Installation

  1. Install thinkfan from repository:
    sudo apt-get install thinkfan
  2. Enable fan control in thinkpad_acpi module configuration:
    sudo echo "options thinkpad_acpi fan_control=1" > /etc/modprobe.d/thinkpad_acpi.conf
  3. Reload thinkpad_acpi module:
    sudo rmmod thinkpad_acpi; sudo modprobe thinkpad_acpi
  4. Set to start thinkfan automatically at boot time:
    sudo vi /etc/defaults/thinkfan
    START=yes

II.Customization

  1. Install lm-sensors:
    sudo apt-get install lm-sensors
  2. To check the temperatures of the sensors:
    sensors
  3. To modify the thinkfan fan control settings:
    sudo vi /etc/thinkfan.conf