Twitter (not set) http://www.janosgyerik.com/blog http://www.janosgyerik.com/

0

### Create an array of CPU frequencies in GHz

` \$ cpus=(\$({ echo scale=2; awk '/cpu MHz/ {print \$4 " / 1000"}' /proc/cpuinfo; } | bc))`

#### Explanation

• The `awk` command takes the input from `/proc/cpuinfo`, matches lines containing `"cpu MHz"`, and appends the `" / 1000"` to the CPU frequency, so it's ready for piping to `bc`
• The `echo scale=2` is for `bc`, to get floating point numbers with a precision of maximum two decimal points
• Group the `echo scale=2` and the `awk` for piping to `bc`, by enclosing the commands within `{ ...; }`
• Run the commands in a `\$(...)` subshell
• Wrap the subshell within `(...)` to store the output lines as an array

From the `cpus` array, you can extract the individual CPU values with:

``````cpu0=\${cpus[0]}
cpu1=\${cpus[1]}
cpu2=\${cpus[2]}
cpu3=\${cpus[3]}
``````

If you don't need the values in GHz, but MHz is enough, then the command is a lot simpler:

``````cpus=(\$(awk '/cpu MHz/ {print \$4}' /proc/cpuinfo))
``````

#### Limitations

Arrays are Bash specific, might not work in older `/bin/sh`.

`/proc/cpuinfo` exists only in Linux.

0

### Test git archive before actually creating an archive // fake dry run

` \$ git archive master some/project/subdir | tar t`

#### Explanation

`git archive` doesn't have a `--dry-run` flag, and it would be nice to see what files would be in the archive before actually creating it.

• `git archive master some/project/subdir`
• Create an archive from the `master` branch, with only a specified sub-directory of the project in it (instead of the entire repo)
• Note: without specifying a file, the archive is dumped to standard output
• `tar t` : the `t` flag of `tar` is to list the content of an archive. In this example the content comes from standard input (piped from the previous command)

In other words, this command creates an archive without ever saving it in a file, and uses `tar t` to list the contents. If the output looks good, then you can create the archive with:

``````git archive master -o file.tar some/project/subdir
``````