54 lines
1.5 KiB
Markdown
54 lines
1.5 KiB
Markdown
---
|
|
title: bash function and awk
|
|
date: 2018-05-31 00:00:48
|
|
tags:
|
|
- bash
|
|
---
|
|
|
|
|
|
|
|
I'll come across many hg branch-switching or log searching tasks during my work. Using bash functions and awk greatly reduce the time I spend on dealing with these tasks. So let's see what these functions can do to help me improve my productivity.
|
|
|
|
|
|
|
|
#### bash functions
|
|
|
|
Bash functions are scripts like `alias`, but can do much a lot than aliasThe first kind of usages of function is to run several scripts continuously, the same as `&&` I guess:
|
|
|
|
```bash
|
|
function run {
|
|
source ~/Develop/django/bin/activate
|
|
./manage.py runserver
|
|
}
|
|
```
|
|
|
|
I activate django virtural enviroment first and then run django serve.
|
|
|
|
Functions, like in any other languages, can take parameters and returns a result. So I can use this ability to do a liitle more complex work:
|
|
|
|
```bash
|
|
function ba { hgba | grep "$1" }
|
|
# hgba is an alias for hg branches
|
|
```
|
|
|
|
I can just type `ba release` then get current release branch info.
|
|
|
|
-----
|
|
|
|
Append:
|
|
|
|
I made some updates to the `ba` function and make it auto copying the branch result to my clipboard:
|
|
|
|
```bash
|
|
|
|
function ba { var="$(hgba | grep $1)" && echo $var | awk -F ':' END{print} | awk -F ':' '{print $NF}' | tr -d '\n' | pbcopy && echo $var }
|
|
```
|
|
|
|
|
|
|
|
#### awk
|
|
|
|
`awk` is a command I just know recently. I need to process a bunch of logs and analyze them. What I used to do is download the logs, grep things I want into a txt file and then process the txt file with python.
|
|
|
|
|