Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Ten to improve the efficiency of the Linux bash tricks     - SUSE Firewall Configuration (Linux)

- Oracle to start to solve the error ORA-27102 (Database)

- Vim useful plugin: EasyGrep (Linux)

- systemd Power Management (Linux)

- Difference between TCP and UDP protocols (Linux)

- ORA-14400: inserted partition key does not map to any partition (Database)

- TNS-03505 name could not be resolved (Database)

- Remote database using RMAN recovery test (RAC return to single-instance database) (Database)

- Du and df show disk space usage inconsistent Causes and Treatment (Linux)

- Linux shell string interception and stitching (Linux)

- Java multi-threaded shared communications variables (Programming)

- Chromium Install Flash Official Guide (Linux)

- Ubuntu 14.04 Boot Repair (Linux)

- Efficient running Linux virtual machine Six Tips (Linux)

- Ubuntu 14.04 / 14.10 how to install FFmpeg 2.5.1 (Linux)

- 64-bit Oracle Linux recompiled Hadoop-2.2.0 (Server)

- Ubuntu study notes and related problem solving (Linux)

- blecat: Bluetooth Gadgets (Linux)

- The headers for the current running kernel were not found when VirtualBox installation enhancements (Linux)

- JSON data normalization (normalize) (Programming)

 
         
  Ten to improve the efficiency of the Linux bash tricks
     
  Add Date : 2018-11-21      
         
         
         
  I like studying bash environment. In many cases, the use bash programming, some of the problems encountered over and over again. Every time I need to rethink solutions to these problems. Until one day I can not stand, then sit down and write a generic function, into my .bashrc file and deployed to the computer.

I hope to pursue these efforts to maximize the efficiency of the results of the command line can also give other friends like to use bash to bring some help. I have greater expectations of my other friends such behavior can cause interactions - give me suggestions, come up with better bash tricks

Tips 1, the command line to the top of the file add text

Every time I will be looking to re-wording of this command. Here's how to use sed to the top of the file to add a line of approach:

sed -i '1s / ^ / line to insert \ n /' path / to / file / you / want / to / change.txt

Tip 2, use the command line to the configuration file to insert multiple lines of text

This method is very simple, a lot of people know, here is how to use the command line (>>) to insert a multi-line text file. As used herein, the "here document" syntax, it allows you to block text symbols by the paragraph insert a file, usually in line with the EOF (which means "End Of File"):

cat >> path / to / file / to / append-to.txt << "EOF"
export PATH = $ HOME / jdk1.8.0_31 / bin: $ PATH
export JAVA_HOME = $ HOME / jdk1.8.0_31 /
EOF
All content will be two "EOF" between being added to the file.

Tip 3, use the command line recursively global search and replace file directory

If you use Eclipse, ItelliJ or other IDE, powerful refactoring capabilities of these tools may make you easily achieve a lot of things. But I guess a lot of time in your development environment is no such integration tools.

How to use the command line to perform a recursive directory search and replace? Do not try to Perl, you can use the find and sed. Thanks for the guidance provided by Stack Overflow:

# OSX version
find. -type f -name '* .txt' -exec sed -i '' s / this / that / g {} +
After use for some time, I wrote a summary function, added to the .bashrc, like this:

function sr {
    find. -type f -exec sed -i '' s / $ 1 / $ 2 / g {} +
}
You can use it like this:

sr wrong_word correct_word

Tip 4, using the command line vim and open a temporary file in Dropbox

I used to like to use Emacs in the scratch facility functions. Vim also often used to quickly create temporary files. Below these two functions is to use openssl to generate a random string as the file name:

function sc {
  gvim ~ / Dropbox / $ (openssl rand -base64 10 | tr -dc 'a-zA-Z'). txt
}

function scratch {
  gvim ~ / Dropbox / $ (openssl rand -base64 10 | tr -dc 'a-zA-Z'). txt
}
In the command line window, type sc or scratch, a new gvim or macvim window will pop up, which will load a random file name of the temporary file.

Tip 5, use the command line to download the file, including a link steering, HTTPS encryption and security situation.

Output terminal to download a page, follow the link steering, ignore the security exception:

curl -Lks
A download link, follow the link steering, ignore the security exception:

curl -OLks
Here with a lot of parameters, you can read this document to learn simple curl them.

Tips 6, Bashmarks

You have not used bashmarks in .bashrc, too? So what are you waiting for? It's really very useful. It can help you keep the history of the operation, jump back to the directory you use most often. Here is my profile in the script, but I think the above link you can provide more tips:

# USAGE:
# S bookmarkname - saves the curr dir as bookmarkname
# G bookmarkname - jumps to the that bookmark
# G b [TAB] - tab completion is available
# L - list all bookmarks

# Save current directory to bookmarks
touch ~ / .sdirs
function s {
  cat ~ / .sdirs | grep -v "export DIR_ $ 1 ="> ~ / .sdirs1
  mv ~ / .sdirs1 ~ / .sdirs
  echo "export DIR_ $ 1 = $ PWD" >> ~ / .sdirs
}

# Jump to bookmark
function g {
  source ~ / .sdirs
  cd $ (eval $ (echo echo $ (echo \ $ DIR_ $ 1)))
}

# List bookmarks with dirnam
function l {
  source ~ / .sdirs
  env | grep "^ DIR_" | cut -c5- | grep "^ * =."
}
# List bookmarks without dirname
function _l {
  source ~ / .sdirs
  env | grep "^ DIR_" | cut -c5- | grep "^ * =." | cut -f1 -d "="
}

# Completion command for g
function _gcomp {
    local curw
    COMPREPLY = ()
    curw = $ {COMP_WORDS [COMP_CWORD]}
    COMPREPLY = ($ (compgen -W ' `_l`' - $ curw))
    return 0
}

# Bind completion command for g to _gcomp
complete -F _gcomp g

Tip 7, extract a (awk technique I use most often) from formatted output in

I will use it almost every day. Really. Often there will be some output, I only need one second row or third column, the following command will be able to do this:

#Sample Output of git status -s command:

$ Git status -s

M .bashrc
?? .vim / Bundle / extempore /

# Remove status code from git status and just get the file names
$ Git status -s | awk '{print $ 2}'

.bashrc
.vim / bundle / extempore /
Why do not you write a function, we can always use it?

function col {
  awk -v col = $ 1 '{print $ col}'
}
This makes it very easy to extract column, for example, you do not want the first column? simple:

$ Git status -s | col 2

.bashrc
.vim / bundle / extempore /

8 Tips to ignore the first x words

I was fascinated to xargs, I feel it's like a sharp knife. But sometimes it used the results obtained need to be adjusted, may need to get some values. For example, the image file you want to get rid of some of the information in the following:

function skip {
    n = $ (($ 1 + 1))
    cut -d '' -f $ n-
}
Here is how to use it:

Use docker images get the following output:
$ Docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
65a9e3ef7171 3 weeks ago 1.592 GB
7c01ca6c30f2 3 weeks ago 11.1 MB
9518620e6a0e 3 weeks ago 7.426 MB
430707ee7fe8 3 weeks ago 7.426 MB
boot2docker / boot2docker latest 1dbd7ebffe31 3 weeks ago 1.592 GB
spaceghost / tinycore-x86_64 5.4 f47686df00df 7 weeks ago 11.1 MB
durdn / bithub latest df1e39df8dbf 8 weeks ago 100.9 MB
c5e6cf38d985 8 weeks ago 100.9 MB
nginx latest e426f6ef897e 12 weeks ago 100.2 MB
zoobab / tinycore-x64 latest 8cdd417ec611 8 months ago 7.426 MB
scratch latest 511136ea3c5a 20 months ago 0 B
Using the above function, you can get all IDs:
$ Docker images | col 3

IMAGE
65a9e3ef7171
7c01ca6c30f2
9518620e6a0e
430707ee7fe8
1dbd7ebffe31
f47686df00df
df1e39df8dbf
c5e6cf38d985
e426f6ef897e
8cdd417ec611
511136ea3c5a
Further processing:
docker images | col 3 | xargs

IMAGE 65a9e3ef7171 7c01ca6c30f2 9518620e6a0e 430707ee7fe8 1dbd7ebffe31 f47686df00df df1e39df8dbf c5e6cf38d985 e426f6ef897e 8cdd417ec611 511136ea3c5a
But in front of the "IMAGE" I also want to get rid of characters:
docker images | col 3 | xargs | skip 1

65a9e3ef7171 7c01ca6c30f2 9518620e6a0e 430707ee7fe8 1dbd7ebffe31 f47686df00df df1e39df8dbf c5e6cf38d985 e426f6ef897e 8cdd417ec611 511136ea3c5a
Complete write down is this:
docker rmi $ (docker images | col 3 | xargs | skip 1)

9 tips to create your own command packet

In bash, you can easily create your own command component, you can look at what I wrote the following:

function dur {
  case $ 1 in
  clone | cl)
    git clone git@bitbucket.org: nicolapaolucci / $ 2.git
    ;;
  move | mv)
    git remote add bitbucket git@bitbucket.org: nicolapaolucci / $ (basename $ (pwd)) git.
    git push --all bitbucket
    ;;
  trackall | tr)
    #track all remote branches of a project
    for remote in $ (git branch -r | grep -v master); do git checkout --track $ remote; done
    ;;
  key | k)
    #track all remote branches of a project
    ssh $ 2 'mkdir -p .ssh && cat >> .ssh / authorized_keys' <~ / .ssh / id_rsa.pub
    ;;
  fun | f)
    #list all custom bash functions defined
    typeset -F | col 3 | grep -v _ | xargs | fold -sw 60
    ;;
  def | d)
    #show definition of function $ 1
    typeset -f $ 2
    ;;
  help | h | *)
    echo "[dur] dn shell automation tools"
    echo "commands available:"
    echo "[cl] one, [mv | move]"
    echo "[f] fun lists all bash functions defined in .bashrc"
    echo "[def] < fun > lists definition of function defined in .bashrc"
    echo "[k] ey < host > copies ssh key to target host"
    echo "[tr] ackall], [h] elp"
    ;;
  esac
}
Through the above script, I can ssh key copied to any server - simply type dur key
user @ somehost.
     
         
         
         
  More:      
 
- To access an Oracle database using Instant Client (Database)
- CRF ++ Linux use (Linux)
- Based Docker build stand-alone high-availability cluster Hadoop2.7.1 Spark1.7 (Server)
- Linux /var/spool/ insufficient clientmqueue space solutions (Linux)
- Linux operating system security management skills (Linux)
- Making Linux root file system problems on-link library (Programming)
- Java reflection Introduction (Programming)
- Gitlab installation under CentOS 7 (Linux)
- Linux Oracle delete archived logs (Database)
- ORA-27054 NFS problem solving (Database)
- Installation through the network Debian 7 (Wheezy) (Linux)
- Ubuntu installation under Scrapy (Linux)
- JavaScript prototype and prototype chain and project combat (Programming)
- Restore Oracle Database Cold backup and database reconstruction emca (Database)
- Linux System Getting Started Learning: install software packages on Ubuntu and Fedora (Linux)
- Source MongoDB 3.2.1 installed on CentOS6.5 (Database)
- Elixir: the future of programming languages (Programming)
- Ceph cluster disk is no workaround for the remaining space (Server)
- Oracle Database Restore (Database)
- Wireless LAN security solutions (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.