Water ~

Here’s a script for the first nuggets article

The main function of this script is to convert the percentage to hexadecimal.

Reason: The design submitted to Lanhu by our design leaders is in percentage opacity control color, whereas Android uses hexadecimal ARGB. That’s where this script comes in

To create a.shfile

For example, run tran_calc.sh. Then copy and paste the following:

#! /bin/bash

# calculated 0% ~ 100%
tran_table(){
    tran=0
    while((tran <= 100))
    do
        tran_to_hex $tran;let "tran++"
    done

}

Calculate and format the output hex
tran_to_hex() {For example: tran_to_hex FF $1 refers to FF. 【 Parameter 2】
    tran=The $1
    temp=$((255*$tran/100))
    Calculate the HEX value and add the leading 0
    hexStr=$(echo "obase=16;$temp"|bc|awk '{ len = (2 - length % 2) % 2; printf "%.*s%s\n", len, "00000000", $0}')
    # Format output
    printf "Transparency %3s% 3s%% Hex %s \n" $temp $tran $hexStr

}

# Execute the then code when there are more than two arguments
if test $# -ge 2
then
    When the first argument is hex, execute the then code
    if test The $1 = "hex"
    then
        # Call the function with [argument 2]
        tran_to_hex $2
    else
        tran_table
    fi
else
    If there are less than two parameters, output the mapping table
    tran_table
fi

Copy the code

Add executable permission

chmod a+x tran_calc.sh
Copy the code

Creating a soft link

The terminal executes the following command

ln -s `pwd`/tran_calc.sh /usr/local/bin/tran_calc
Copy the code

use

Now you can execute at the terminal

  • Output 0%~100% transparency CRT
tran_calc
Copy the code

or

  • Converts numeric values to hexadecimal
Tran_calc hex [value]Copy the code

For example, tran_calc HEX 50 calculates the HEX value for 50% transparency

Expand the knowledge

The Shell passes the parameters. Take a look at the following table from the passing parameters section of the novice tutorial.

writing meaning
$# Number of arguments passed to the script
$* Displays all parameters passed to the script as a single string. Such as”
With” “In brackets, to *” where “is enclosed in” “,”
1
2 2…
Output all parameters in the form n”.
$$ ID of the process where the script is running
$! The ID number of the last process running in the background
$@ with
Same, but in quotes and return each parameter in quotes. Such as * Same, but in quotes, and returns each argument in quotes. Such as”
@” In parentheses (” “), with”
1 1″ ”
2 “… Output all parameters in the form “$n”.
$- Displays the current options used by the Shell, andThe set commandSame function.
$? Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error.

I wanted to see if there was a chance to participate in the next August. Small words can not participate, there is a large section of code can not participate, there is a large number of references can not participate. Ha, ha, ha. I took it all. Forget it.

Will see. Welcome correction ~~