Disable a few cores when you want to save power.

If you have a lot of CPU power and working on battery. If you do not need that much of power and would like to rather save some battery power by disabling some cpus then you can use the below script. This script disables cpus from 4 to 7. You can change the number in the for loop. You would need the sudo to be setup or remove sudo and run the script as root. The script will show you the currently active cpu’s before and after disabling the CPU’s.

If you want to disable to cpus from the grub itself then you can add the below parameter in grub.cfg or grub2.cfg in the vmlinuz line:

1
maxcpus=4

Here is the script:

#!/bin/bash -
#===============================================================================
#
#          FILE: disablecpu.sh
#
#         USAGE: ./disablecpu.sh
#
#   DESCRIPTION:
#
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: Amit Agarwal (aka)
#      REVISION:  ---
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
cat /proc/cpuinfo|grep cores|wc -l
for i in {4..7}
do
    sudo sh -c "echo 0 > /sys/devices/system/cpu/cpu$i/online"
done
cat /proc/cpuinfo|grep cores|wc -l
Enhanced by Zemanta

Array of all possible colors in bash script

Sometime back, we had looked at using colors in bash script and here is a way to create a array of all the colors.

#!/bin/bash - 
#===============================================================================
#
#          FILE:  colorarray.sh
# 
#         USAGE:  ./colorarray.sh 
# 
#   DESCRIPTION:  
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: Amit Agarwal (aka), amit.agarwal@roamware.com
#       COMPANY: Roamware India Pvt Ltd
#       CREATED: 16/04/12 17:24:09 IST
# Last modified: Mon Apr 16, 2012  05:39PM
#      REVISION:  ---
#===============================================================================
count=0
for i in 40m 41m 42m 43m 44m 45m 46m 47m
do
    for l in 0 1
    do
        echo -n "l=$l "
        for k in 0 1
        do
            # echo -n "k=$k "
            for j in {30..37}
            do
                carr[$count]="\033[$k;$j;$l;$i"
                echo -ne "${carr[$count]}$count \033[00m"
                ((count++))
            done
        done
        echo
    done
    echo
done

You can add this script to any script of your choice and you have all the 256 colors available to you in the script without additional coding. Happy coding.

Enhanced by Zemanta

colors in bash scripts

Colours
Colours (Photo credit: CherrySoda!)

I have been trying to understand the color codes for bash for a pretty long time, but somehow never got time to understand this clearly. So this time around when I was writing a script to analyze some logs. I thought I will give it a go and finally understood some part of this.

So, first we will start with this script. This is taken from here.

#!/bin/bash -
#===============================================================================
#
#          FILE:  colortest.sh
#
#         USAGE:  ./colortest.sh
#
#   DESCRIPTION:   http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: Amit Agarwal (aka), amit.agarwal@roamware.com
#       COMPANY:
#       CREATED: 09/04/12 14:29:47 IST
# Last modified: Sun Apr 15, 2012  06:29PM
#      REVISION:  ---
#===============================================================================

#!/bin/bash
#
#   This file echoes a bunch of color codes to the
#   terminal to demonstrate what's available.  Each
#   line is the color code of one forground color,
#   out of 17 (default + 16 escapes), followed by a
#   test use of that color on all nine background
#   colors (default + 8 escapes).
#

T='gYw'   # The test text

echo -e "\n                 40m     41m     42m     43m\
     44m     45m     46m     47m";

for FGs in '    m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' \
           '1;32m' '  33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' \
           '  36m' '1;36m' '  37m' '1;37m';
  do FG=${FGs// /}
  echo -en " $FGs \033[$FG  $T  "
  for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
    #do echo -en "$EINS \033[$FG\033[$BG  \$T  \033[0m";
    do echo -en " \033[$FG\033[$BG  $T  \033[0m";
  done
  echo;
done
echo

This script will give you all the colors that are possible in the bash scripting. And now let’s understand color’s a little more.

There are basically two ways you can use colors and I will illustrate both with echo command.

Before we get into all these, we will see hot to reset the colors:

echo -e "\033[00m"

The first one is the one used in the above script, where in you can use the codes multiple times to get the desired effect. For example, see the screenshot below:

colors in bash
colors in bash

And the other way to achieve the same is to use all the color codes with one escape sequence :

colors in bash
colors in bash

Important to note here is that the “m” is missing.

All the codes that you want can be used separated by “;” and end with “m”. I think I have explained the things.

Enhanced by Zemanta