How do I enter the results of a shell command line line by line?


must

I am learning shell scripting. I want to check the size of directories (and files). I made a sample script

#!/bin/bash

for dir in $(du ".")
do
  echo "dir = $dir"
  read T_SIZE T_PATH <<< $dir
  echo "T_SIZE = $T_SIZE"
  echo "T_PATH = $T_PATH"
done

The directory structure under the current directory has only one directory "a".

The result is

dir = 4
T_SIZE = 4
T_PATH = 
dir = ./a
T_SIZE = ./a
T_PATH = 
dir = 16
T_SIZE = 16
T_PATH = 
dir = .
T_SIZE = .
T_PATH = 

It seems that the command "du" gives the result verbatim. However, I want the command "du" to give the result line by line, and I want the result of the script above to be

dir = 4
T_SIZE = 4
T_PATH = ./a
dir = 16
T_SIZE = 16
T_PATH = .

Please tell me any questions or suggestions for me.

thank you very much.

John 1024

To read the output of a command duline by line , try:

du . | while read size path
do
   echo "size=$size path=$path"
done

Alternatively, if the shell used (like bash) supports process substitution:

while read size path
do
   echo "size=$size path=$path"
done < <(du ".")

There is a big difference between the two methods. In the first whileexample above, the loop is running in a subshell, so when the subshell finishes, all its shell variable variables will disappear. In the code behind , the shell variable is still available after the whileloop completes .

Related


How do I enter multiple karma reporters in the command line?

starlight Is it possible to enter multiple reporters that I want to use from the command line? I have tried the following combinations and it doesn't seem to work > karma start karma.conf.js --reporters progress summary > karma start karma.conf.js --reporters

How do I enter multiple karma reporters in the command line?

starlight Is it possible to enter multiple reporters that I want to use from the command line? I have tried the following combinations and it doesn't seem to work > karma start karma.conf.js --reporters progress summary > karma start karma.conf.js --reporters

How do I enter multiple karma reporters in the command line?

starlight Is it possible to enter multiple reporters that I want to use from the command line? I have tried the following combinations and it doesn't seem to work > karma start karma.conf.js --reporters progress summary > karma start karma.conf.js --reporters

How do I enter multiple karma reporters in the command line?

starlight Is it possible to enter multiple reporters that I want to use from the command line? I have tried the following combinations and it doesn't seem to work > karma start karma.conf.js --reporters progress summary > karma start karma.conf.js --reporters

How do I enter multiple karma reporters in the command line?

starlight Is it possible to enter multiple reporters that I want to use from the command line? I have tried the following combinations and it doesn't seem to work > karma start karma.conf.js --reporters progress summary > karma start karma.conf.js --reporters

How do I enter multiple karma reporters in the command line?

starlight Is it possible to enter multiple reporters that I want to use from the command line? I have tried the following combinations and it doesn't seem to work > karma start karma.conf.js --reporters progress summary > karma start karma.conf.js --reporters

How do I enter multiple karma reporters in the command line?

starlight Is it possible to enter multiple reporters that I want to use from the command line? I have tried the following combinations and it doesn't seem to work > karma start karma.conf.js --reporters progress summary > karma start karma.conf.js --reporters

How to enter an existing R shell from the command line Linux

Aaron all, I am accessing a remote server via SSH and have an R process running on that server. We're in development right now, so it's important for me to enter the R shell and run the commands line by line. Last night I logged into the server, sent a bunch o

How to enter an existing R shell from the command line Linux

Aaron all, I am accessing a remote server via SSH and have an R process running on that server. We're in development right now, so it's important for me to enter the R shell and run the commands line by line. Last night I logged into the server, sent a bunch o

How do I change the command line executable?

Mayak Agrowal When I opened Sublime Text from the terminal, I typed $ sublime_text <filename> Is there any way to change it so I can say this: $ subl <filename> heme Creating an alias and putting it in ~/.bashrcwill do the following: printf "\nalias subl=$(w

How do I get C:/ on the command line

username I am on Windows 10. I'm trying to load Adobe Dreamweaver extension via command line on Windows. I want to navigate to C:\Program Files\Adobe\Adobe Extension Manager CSx\ I am following the instructions here . If I select command prompt (admin), I getC

How do I get C:/ on the command line

username I am on Windows 10. I'm trying to load Adobe Dreamweaver extension via command line on Windows. I want to navigate to C:\Program Files\Adobe\Adobe Extension Manager CSx\ I am following the instructions here . If I select command prompt (admin), I getC

How do I change the command line executable?

Mayak Agrowal When I opened Sublime Text from the terminal, I typed $ sublime_text <filename> Is there any way to change it so I can say this: $ subl <filename> heme Creating an alias and putting it in ~/.bashrcwill do the following: printf "\nalias subl=$(w

mongo command line vs shell: different results

Shabbir Safdar I'm confused about Mongo Shell making different changes on output. When I log into the mongo shell and run the simplest query like: db.database.findOne(); I got the returned documents. When I invoke the same command via the --eval option on the

mongo command line vs shell: different results

Shabbir Safdar I'm confused about Mongo Shell making different changes on output. When I log into the mongo shell and run the simplest query like: db.database.findOne(); I got the returned documents. When I invoke the same command via the --eval option on the

How can I enter 2 commands on the Windows command line?

John McIntyre At the DOS command line, I used to be able to enter ¶ between commands to put multiple commands on one line. For example, don't type c:\> cls c:\> cd i can enter c:\> cls¶cd Has this feature been removed or replaced with something else? How to

How can I enter 2 commands on the Windows command line?

John McIntyre At the DOS command line, I used to be able to enter ¶ between commands to put multiple commands on one line. For example, don't type c:\> cls c:\> cd i can enter c:\> cls¶cd Has this feature been removed or replaced with something else? How to

How can I apply a shell command to every line of command output?

Alex Budowski Suppose I get some output from a command (for example ls -1) : a b c d e ... I want to apply a command (for example ) echoto each command in turn . E.g echo a echo b echo c echo d echo e ... What's the easiest way to do this in bash? Michael Mo

How can I apply a shell command to every line of command output?

Alex Budowski Suppose I get some output from a command (for example ls -1) : a b c d e ... I want to apply a command (for example ) echoto each command in turn . E.g echo a echo b echo c echo d echo e ... What's the easiest way to do this in bash? Michael Mo

How to enter multiple stdin in one line using php command line

Scalable like Input is: 3 1 2 2 3 4 5 I have to accept these inputs in a given style. Here (1 and 2), (2 and 3) and (4 and 5) must accept one line as input. Here is my code snippet. <?php header('Content-Type: text/plain'); $testCase = (int) fgets(STDIN);

How to enter multiple stdin in one line using php command line

Scalable like Input is: 3 1 2 2 3 4 5 I have to accept these inputs in a given style. Here (1 and 2), (2 and 3) and (4 and 5) must accept one line as input. Here is my code snippet. <?php header('Content-Type: text/plain'); $testCase = (int) fgets(STDIN);

How to enter multiple stdin in one line using php command line

Scalable like Input is: 3 1 2 2 3 4 5 I have to accept these inputs in a given style. Here (1 and 2), (2 and 3) and (4 and 5) must accept one line as input. Here is my code snippet. <?php header('Content-Type: text/plain'); $testCase = (int) fgets(STDIN);