Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Shell script on the variables with double quotation marks grep small problem     - Spark local development environment to build (Server)

- Plasma 5.4 How to install on Kubuntu 15.04 (Linux)

- Android memory optimization of the optimal load Bitmap (Linux)

- expdp / impdp use version parameter data migration across versions (Database)

- How to install Bugzilla 4.4 on Ubuntu / CentOS 6.x (Linux)

- To install and configure the Jetty server and JDK under Ubuntu 14.04.2 (Server)

- CentOS 6.4 Python 2.6 upgrade to 2.7 (Linux)

- Getting Started with Linux system to learn: how to check the version of SSH on Linux (Linux)

- How VMware extend disk without rebooting the system (Linux)

- Python 3.5 await / async (Programming)

- Cobbler remotely install CentOS system (Linux)

- ASP.NET 5 is connected with the Redis server on the Linux platform (Server)

- Sublime Text - Opens the current file in a browser (Linux)

- Let's Encrypt with semiautomatic into Nginx configuration https (Server)

- Linux Beginner Guide: Installing packages on Ubuntu and Fedora (Linux)

- Linux shell script debugging (Linux)

- ORA-00845: MEMORY_TARGET not supported on this system Problem (Database)

- How to convert images, audio and video formats on Ubuntu (Linux)

- Java singleton mode (Singleton mode) (Programming)

- A new method for Linux hidden files (Linux)

  Shell script on the variables with double quotation marks grep small problem
  Add Date : 2018-11-21      
  Today, when writing a shell script, there is an operation is to use the grep command in a file search for the specified content. Specify the contents stored in the file, use a variable to get the contents of the file, and then go to the grep command.

    This code is as follows:
For target in `cat content.txt`
    Grep $ target test.txt >> result.txt
Content.txt The contents of the text are:
"Domain \ [2 \]"
"Domain \ [3 \]"
"Domain \ [4 \]"
    Because the use of grep, the final order to be implemented (grep "domain \ [2 \]" test.txt), so the direct quotation marks written in the content.txt the text. Then the implementation of the script, when the implementation of the script began to be wrong, the direct implementation (grep "domain \ [2 \]" test.txt) is able to search for the content. But in the script is not search for content, so add "set-x" to see the implementation of the order to show the implementation of the order is (grep "domain \ [2 \]" test.txt). Finally tortured for a long time, found that the text is content.txt double quotation marks in the problem. After the success of the implementation of the following change, to search the content.
Content.txt The contents of the text are:
Domain \ [2 \]
Domain \ [3 \]
Domain \ [4 \]
For target in `cat content.txt`
Grep "$ target" test.txt >> result.txt
So thought, but also wrote a script test:
#! / Bin / ksh
Set -x
Grep "\" hello \ "" test.c
This script actually simulates the use of double quotes as part of a variable.
Execution Result The command to be executed is:
+ Grep "hello" test.c
And the above example to see is the same, the surface looks in the test.c search hello, but in fact the search is ( "hello"), so the search can not. Similarly, for the previous example, I think the search is (domain \ [2 \]), in fact, search for ( "domain \ [2 \]"), but ( "domain \ [2 \]") This is indeed No, so not search.
    That is, if the double quotation marks on content.txt passed to the variable, resulting in double quotation marks is no longer the content to be queried, but the double quotation marks is a part of the variable. Finally, although the two see the appearance of exactly the same, but the meaning is not the same. Hey, shell good magic ah, do not believe you see, ha ha.
Explanation: The shell used is ksh.
- Squid proxy server configuration under Linux (Server)
- Linux Firewall IPCop Profile (Linux)
- Swift acquaintance of values and references, the circular reference, Agent Precautions (Programming)
- Broadcom transplanted to OpenWrt summary (Programming)
- Introduction and bash history command to quickly call (Linux)
- Turning off the interface eth0: error: Disconnect the device 'eth0' (Linux)
- Use Oracle 11g show spparameter command (Database)
- MySQL Tutorial: Some optimized for I / O memory areas (Database)
- Hard disk encryption to protect data security (Linux)
- MySQL thread cache thread_cache_size parameter optimization (Database)
- Python developer of time-saving method (Programming)
- Linux virtual memory and physical memory (Linux)
- Everyone should know something about TCP (Linux)
- FreeBSD install Gnome 3 Desktop (Linux)
- Ubuntu 14.04 / 13.10 users how to install Ubuntu Touch core applications (Linux)
- Java interface and inheritance (Programming)
- Intrusion prevention network server security maintenance tips (Linux)
- Nginx reverse proxy and self-signed https (Server)
- Servlet life cycle code examples (Programming)
- Repair fatal error in Linux: lame / lame.h: No such file or dir Error (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.