An issue with ls and wc commands in Linux-Linux general technology-Linux technology and application information. For more information, see the following. Here is a question about the ls and wc commands:
[Qianxb @ aa qianxb] $ cat a. sh
#! /Bin/bash
Num = 'ls | wc-l'
New = q $ num
Echo $ new
The execution result is "q 2" with a space in the middle.
There are two problems in the middle:
1) ls displays only one row of results, but wc returns two rows.
2) there is a space in the middle of the final echo result
Question 1:
Execute the ls command, and the display is as follows:
[Qianxb @ aa qianxb] $ ls
A. sh s. pl
The ls command result is passed to wc in a pipeline. The result is as follows:
[Qianxb @ aa qianxb] $ ls | wc-l
2
The wc results show that there is a newline in the. sh and s. pl files displayed by ls ).
Redirects the ls output result to the file. The result is as follows:
[Qianxb @ aa qianxb] $ ls | tee
A. sh
S. pl
It is determined that although the result output by ls is displayed as a line on the screen, there is actually a line break in the middle.
Question 2:
There is a space in the result displayed by the wc command, so you need to remove it or use other commands.
Num = 'ls | wc-l | tr-d' [: blank:]''
Or
Num = 'ls | wc-l | awk '{print $1 }''
To sum up, modify the program
[Qianxb @ aa qianxb] $ cat a. sh
#! /Bin/bash
Num = 'ls | wc-l | tr-d' [: blank:]''
# Num = 'ls | wc-l | awk '{print $1 }''
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.