why function variable not working in command substitution inside a function? [duplicate] - bash

This question already has answers here:
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 1 year ago.
im running this in bashrc file
function simplefunc () {
output=$(ls -1 "$HOME")
linecount=$(${output} | wc -l)
echo "${linecount}"
echo "${output}"
}
getting this error
Desktop: command not found
0
Desktop
Documents
Downloads
Music
Pictures
Public
snap
SoftMaker
Templates
venv
Videos
i tried these too
putting "local" before variable or
# linecount=$(output | wc -l)
# echo "$(${output} | wc -l)"

I think you should change the third line to:
linecount="$(echo "${output}" | wc -l)"
# OR alternatvely:
# linecount="$(wc -l < <(echo "$output"))"

Related

How to use ` in ` in bash [duplicate]

This question already has answers here:
How to properly nest Bash backticks
(6 answers)
Closed 3 years ago.
Is there a way to use ` inside a ` in bash script
This works
echo `echo '(!1)*2' | bc -l`
but this doesnt
echo `echo '(!`echo 1`)*2' | bc -l`
the error is
unmatched '
how do I fix this?
You can make use of $() instead of ``:
$ echo $(echo $(echo $(echo "Hello") world)! )
Or the solution to your answer:
$ echo $(echo "( ! $(echo 1) )*2" | bc -l)
p.s. don't use ' when you want to make use of variables inside a string. Only double quotes " are parsed.

Run a concatenated string as a command within a Bash script [duplicate]

This question already has answers here:
Variables as commands in Bash scripts
(5 answers)
Closed 3 years ago.
I'm trying to count the number of files with different extensions in /foo/.
case 1 works as expected, but more flexible situations such as case 2 or case 3 don't work as expected.
File test.sh
# case 1
vista=$(find /foo/*.zip -atime -1)
echo "$vista" | wc -l
# case 2
vista=$(find /foo/*)
echo "$vista.zip -atime -1" | wc -l
# case 3
echo "$vista.xz -atime -1" | wc -l
Output:
./test.sh
187
4566
4566
I suspect the problem is that for example echo "$vista.zip -atime -1" from case 2 runs first find /foo/* before appending the string zip -atime -1, but I don't know how to do it right.
Code should never be stored in strings (unless using printf %q to generate eval-safe versions of variables, and then using eval at runtime). Use either an array (for dynamically-constructed content) or a function.
The former:
find_cmd=( find /foo/* )
"${find_cmd[#]}" -atime -1 | wc -l
The latter:
find_under_foo() { find /foo/* "$#"; }
find_under_foo -atime -1 | wc -l

Assigning a command output to a shell script variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 2 years ago.
How do I assign a command output to a shell script variable.
echo ${b%?} | rev | cut -d'/' -f 1 | rev
${b%?} gives me a path..for example: /home/home1
The above command gives me home1 as the output. I need to assign this output to a shell script variable.
I tried the below code
c=${b%?} |rev | cut -d '/' -f 1 | rev
echo $c
But it didn't work.
To assign output of some command to a variable you need to use command substitution :
variable=$(command)
For your case:
c=$(echo {b%?} |rev | cut -d '/' -f 1 | rev)
Just wondering why dont you try
basename ${b}
Or just
echo ${b##*/}
home1
If you want to trim last number from your path than:
b="/home/home1"
echo $b
/home/home1
b=${b//[[:digit:]]/}
c=$(echo ${b##*/})
echo ${c}
home
Just like this:
variable=`command`

Bash for loop stops after one iteration without error [duplicate]

This question already has answers here:
What does set -e mean in a bash script?
(10 answers)
Closed 2 years ago.
Lets say I have a file dates.json:
2015-11-01T12:01:52
2015-11-03T03:58:57
2015-11-09T02:43:59
2015-11-10T08:22:00
2015-11-11T05:14:51
2015-11-11T12:47:02
2015-11-13T08:33:40
I want to separate the rows to different files according to the date.
I made the following script:
#!/bin/bash
set -e
file="$1"
for i in $(seq 1 1 31); do
if [ $i -lt 10 ]; then
echo 'looking for 2015-11-0'$i
cat $file | grep "2015-11-0"$i > $i.json
else
echo 'looking for 2015-11-'$i
cat $file | grep "2015-11-"$i > $i.json
fi
done
When I execute I get the following:
$ bash example.sh dates.json
looking for 2015-11-01
looking for 2015-11-02
If I try without the cat... rows the script prints all the echo commands, and if I try only the cat | grep command on the command line it works.
Would you know why does it behave like this?
If you need set -e in other parts of the script, you need to handle grep not to stop your script:
# cat $file | grep "2015-11-0"$i > $i.json
grep "2015-11-0"$i "$file" > $i.json || :
set -e forces script to exit if command exits with non-zero status.
+
grep returns 1 if it fails to find match in file.
+
dates.json has no 2015-11-02
=
error
it's because of our set -e which causes the script to exit. Remove this line, then it should work

BASH script looking for some guidance [duplicate]

This question already has answers here:
Bash variable scope
(7 answers)
Closed 9 years ago.
well im in this for hours and I can't understand why I cant save the values to the variable:
let inicio=${tlinhas[0]}/2+1
tail -n +$inicio $1 | head -n $tlinhas | grep $2 | while read linha
do
let palavras=$palavras+$(echo $linha | wc -w)
echo $palavras
done
printf "%d" $palavras
the problem is that every time I print the variable palavras its always zero but if I print it inside the while it has the value 14
[leganuno#LegaNuno-PC FichasIndividuais]$ ./exercicio1.sh f1 Licenciatura
7
7
0
Try
palavras = echo $($palavras + $(echo $linha | wc -w) | bc)

Resources