How to line up the contents of a variable [duplicate] - linux

This question already has answers here:
How to join multiple lines of filenames into one with custom delimiter
(22 answers)
Closed 3 years ago.
I run the following commands:
cd /proc
process=$(ls | egrep '[0-9]')
echo $process
I get the following output:
1
108
109
8130
However, I want to have the following output:
1 108 109 8130
How can I do that?

Since your variable process is only be used in the echo, I would simplify your script to
cd /proc
echo *[0-9]*
If you really need the process names for postprocessing in a later step, I would store them in an array:
processes=(*[0-9]*)
With this approach, you can display them in a single line using
echo "${processes[#]}"

The easiest way is to use echo command as:
...
process=$(echo [0-9]*)
...

Related

How to format linux mpstat output in multiple lines [duplicate]

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
Closed 3 years ago.
I have a small script where I appended the output of linux mpstat to a log file.
#/bin/bash
CPU_USAGE=$(mpstat)
echo $CPU_USAGE >> temp.log
The problem is that the output of mpstat on the terminal is formatted properly in 3 lines like so
However, the output to the file is all in one line.
How do I format the output like the one on the terminal?
Just quote the variable so it is not seen as several different parameters to be printed one after the other:
echo "$CPU_USAGE" >> temp.log
You could just directly pipe the output to the file:
#!/bin/bash
mpstat >> temp.log
If you must store it in a variable, then quote it like:
#!/bin/bash
CPU_USAGE=$(mpstat)
echo "$CPU_USAGE" >> temp.log
Otherwise, bash will not interpret the newlines as part of the message to echo, but the whole output as a list of short strings to output.

How to display exactly 10 lines of a command output [duplicate]

This question already has answers here:
How can I view only the first n lines of the file?
(2 answers)
Closed 4 years ago.
I am working on ARM-based processor and I am preparing inside it a shell script that writes into a text file a set of commands output.
I want it to write exactly 10 lines of a command's output (for example top command) but I don't know how, would you help me please ?
Thank you.
Which operating system are you working in ? If you have awk installed, you
can do:
command | awk 'NR<=10' > f.txt
command | head -n 10 > file.txt
If you want a pure Bash solution:
n=0
command | while (( n++ != 10 )) && IFS= read -r line; do
printf '%s\n' "$line"
done
command | sed 1,10p > f.txt
sed filters lines based on a pattern and performs an action on them. In this case, pattern is to filter lines whose number is between 1 and 10, and action is just to 'p'rint them.

Syntax error: operand expected (error token is “+”) (i coudn't resolve this problem with the other thread) [duplicate]

This question already has answers here:
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
How to loop over directories in Linux?
(11 answers)
How to assign the output of a Bash command to a variable? [duplicate]
(5 answers)
Closed 4 years ago.
I cant get this simple program working the code looks like this because is for a class exercise I wouldn't do it like that but I have to, sorry if the code is a mess but I've tried so many things that the code is a bit "deformed"
n= 0
for x in /home
do
e= du $x -B1 | cut d" " -f 1
$sum$(($sum+$e))
done
echo $sum
At line 1 you have a space between the '=' and the 0, but there shouldn't be a spaces either before or after the '=' in an assignment.
At line 4 happens the same, but also you missed the backticks '`' around the commands, that indicate bash to evaluate what is inside the backticks and return the output of that command.
At line 5 it says:
$sum$(($sum+$e))
So did you mean:
sum=$(($sum+$e))
Update: I have found three problems more:
In line 2, replace /home with /home/*, because the former only uses /home in the loop, and the later returns every directory (and file) in the /home directory.
You are passing d" " to cut, the correct option is -d " ".
Also, du output is formatted with tabs, not spaces. If you delete the -d " " in cut, it works.

How to Save 'specific' line from terminal output to file? [duplicate]

This question already has answers here:
Bash tool to get nth line from a file
(22 answers)
Closed 4 years ago.
I am currently using the following to save terminal outputs to file:
$command -someoptions >> output.txt
However, I am only interested in one line from the terminal output.
Is there a way to do this by changing the above expression. Or will I have to delete lines after the 'output.txt' file is formed?
For example: If my output is:
line 1
line 2
line 3
line 4
line 5
and all I want to save is:
line 4
where line 4 contains unknown information.
I am asking as I will later wish to script this command.
Many thanks,
Solution Found:
I ended up using:
$command -someoptions | sed -n '4p' >> output.txt
This is a classic simple grep issue.
$command -someoptions | grep 'line 4' >> output.txt
You could refine that with more pattern complexity, and might need it depending on how precisely you need to match the data.
Try with this command:
$command -someoptions | grep " filter " >> output.txt
filter must be replaced by an element that distinguishes your line 4 from the other lines.

How can I extract a specific number from df in bash? [duplicate]

This question already has answers here:
How can I *only* get the number of bytes available on a disk in bash?
(7 answers)
Closed 6 years ago.
My aim is checking if there is still enough space on my disk, every time my script (bash) proceeds a step.
Running df; echo $? prints:
Dateisystem 1K-Blöcke Benutzt Verfügbar Verw% Eingehängt auf
/dev/sdc4 1869858440 1680951776 93900284 95% /mnt/dd
0
The 0 is the result of that command.
In my case, I only want 93900284 in a variable or as the result.
I already read man df.
df --output=avail /path/to/where/you/want/to/write | tail -n 1
BTW: bash 'returns' (in this case 0 == success) are exit codes, the way you phrase it it seems you try to capture that rather than the output. In that case, you might want to read this.
You can use awk to extract suitable field from output:
BASH_VAR=`df | awk '/\/dev\/sda4/{print $4;}''`
If what you want to do is display only available disk space, you can use the following command
df -k /dev/sdc4 | tail -1 | awk '{print $4}'

Resources