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

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.

Related

Add markdown code tick to beginning and ending of a variable [duplicate]

This question already has an answer here:
How to escape backticks in bash
(1 answer)
Closed 3 months ago.
I searched all forums but didn't find a suitable way to achieve this.
variable
result=multiline
string and
other string
Want to convert it to
```result=multiline
string and
other string```
means add 3 ticks at the beginning and 3 ticks at the end of the file.
I know it's possible through sed, but I think it's not working because of special characters for example tick.
Any advice is appreciated.
Escaping backticks with \ helped.
echo $result > result.txt
echo "\`\`\`" | cat - result.txt > some-result.txt
echo "\`\`\`" >> markdown-result.txt

How to change file parmater by input in bash [duplicate]

This question already has answers here:
How do I use sed to change my configuration files, with flexible keys and values?
(8 answers)
Closed 2 years ago.
I have a file that contain this line
SELINUX = enforce
I want to change this by given input to permissive
How i do that without demage?
If [[ "$1" == "Y"]]
then
sed -ri 's/(^.*SELINUX=)(.*$)/\1enforce/' file
else
sed -ri 's/(^.*SELINUX=)(.*$)/\1permissive/' file
fi
If the first passed parameter ($1) is equal to "Y" use sed to split SELINUX line into to 2 sections. Substitute the line for the first section followed by "enforce". If the passed parameter is not "Y" substitute the line for the first section followed by "permissive".

Replace hyphens with underscores in bash script [duplicate]

This question already has answers here:
Replacing some characters in a string with another character
(6 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
Trying to write a bash script and in one part of it I need to take whatever parameter was passed to it and replace the hyphens with underscores if they exist.
Tried to do the following
#!/usr/bin/env bash
string=$1
string=${string//-/_}
echo $string;
It's telling me that this line string=${string//-/_} fails due to "Bad substitution" but it looks like it should do it? Am I missing something?
There is nothing wrong with your script, and it should work in modern versions of Bash.
But just in case you can simplify that to :
#!/bin/bash
echo "$1" | tr '-' '_'
This is in case that parameter substitution does not work ( which seems to be your case ).
Regards!

BASH: sed to remove "/" from a $STRING [duplicate]

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 7 years ago.
First let me apologize for asking a question that:
Has some examples, though I find them confusing
Has a man page, also find confusing
Problem:
I would like to replace text in a $STRING within bash for a script I am writing. I chose to combine date/time to allow for easier end user integration.
STARTTIME="2015-03-17/11:30:00"
sed "Unknown"
Attempted Solution:
sed '/s// /' "$STARTTIME"
Desired result is to remove the "/" and end up with 2015-03-17 11:30:00 to then be passed to a command.
Thank you for any assistance.
If you're using bash, I would suggest that you used built-in string manipulation:
$ s='2015-03-17/11:30:00'
$ echo "${s/\// }"
2015-03-17 11:30:00
The syntax inside the braces means "replace the first occurrence of a forward slash (which needs escaping) with a space".

How to store and echo multiple lines elegantly in bash? [duplicate]

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
Closed 5 years ago.
I'm trying to capture a block of text into a variable, with newlines maintained, then echo it.
However, the newlines don't seemed to be maintained when I am either capturing the text or displaying it.
Any ideas regarding how I can accomplish this?
Example:
#!/bin/bash
read -d '' my_var <<"BLOCK"
this
is
a
test
BLOCK
echo $my_var
Output:
this is a test
Desired output:
this
is
a
test
You need to add " quotes around your variable.
echo "$my_var"

Resources