bash script assign output to a variable fail - linux

OS:Yocto
I want to assign the shell output to a variable,
but get the error "test.sh: line 3: out: command not found".
How to do that ...?
this is my code:
#!/bin/bash
out = "$(ls /dev/ | grep "tty" | wc -l)"
echo"$out"
I tried this:
How to set a variable to the output from a command in Bash?

Whitespace matters.
#!/bin/bash
out="$(ls /dev/ | grep "tty" | wc -l)"
echo "$out"

when you assign value to variable don't keep Whitespace before and after "=" that makes error in bash
#!/bin/bash
out="$(ls /dev/ | grep "tty" | wc -l)"
echo"$out"

Try strip spaces around =, i.e. out="$(ls /dev/ | grep "tty" | wc -l)"

Related

Linux - How to count occurrences of character 'i' in a word (not a file)

How can I print the number of occurrences of "i" in Pneumonoultramicroscopicsilicovolcanoconiosis?
How can I approach this with the commands grep and wc?
echo "Pneumonoultramicroscopicsilicovolcanoconiosis" | grep -o i | wc -l
This should do it:
echo "Pneumonoultramicroscopicsilicovolcanoconiosis"|tr -cd 'i'| wc -c
$ echo "Pneumonoultramicroscopicsilicovolcanoconiosis" | awk '{print gsub(/i/,"")}'
6

storing the output of ls command in a shell variable

I am using below command to find a most recent file with name "candump"
ls *.log | grep "candump" | tail -n 1
The output is "candump-2018-04-19_131908.log"
I want to store the output filename to a variable in my shell script. I am using the below commands:
logfile = `ls *.log | grep "candump" | tail -n 1`
and
logfile = $(ls *.log | grep "candump" | tail -n 1)
However, both times I am getting the same error, "logfile: command not found". Am I doing something wrong? Any help is appreciated.
You have to stick the variable name and its value (no space before and after the =).
Try :
logfile=$(ls *.log | grep "candump" | tail -n 1)
This is working for me.
#!/bin/bash
my_command='ls | grep server.js | wc -l';
my_data=$(eval "$my_command");
echo "value in echo is:" $my_data;
if [ $my_data == "1" ]; then
echo "value is equal to 1";
else
echo "value is not equal to 1";
fi

Command to count the characters present in the variable

I am trying to count the number of characters present in the variable. I used the below shell command. But I am getting error - command not found in line 4
#!/bin/bash
for i in one; do
n = $i | wc -c
echo $n
done
Can someone help me in this?
In bash you can just write ${#string}, which will return the length of the variable string, i.e. the number of characters in it.
Something like this:
#!/bin/bash
for i in one; do
n=$(echo $i | wc -c)
echo $n
done
Assignments in bash cannot have a space before the equals sign. In addition, you want to capture the output of the command you run and assign that to $n, rather than that statement which would probably just assign $i to $n.
Use the following instead:
#!/bin/bash
for i in one; do
n=`$i | wc -c`
echo $n
done
It can be as simple as that:
str="abcdef"; wc -c <<< "$str"
7
But mind you that end of line counts as a character:
str="abcdef"; cat -A <<< "$str"
abcdef$
If you need to remove it:
str="abcdef"; tr -d '\n' <<< "$str" | wc -c
6

Command substitution as a variable in one-liner

I get the following error:
> echo "${$(qstat -a | grep kig):0:7}"
-bash: ${$(qstat -a | grep kig):0:7}: bad substitution
I'm trying to take the number before. of
> qstat -a | grep kig
1192530.perceus- kigumen lr_regul pbs.sh 27198 2 16 -- 24:00:00 R 00:32:23
and use it as an argument to qdel in openPBS so that I can delete all process that I started with my login kigumen
so ideally, this should work:
qdel ${$(qstat -a | grep kig):0:7}
so far, only this works:
str=$(qstat -a | grep kig); qdel "${str:0:7}"
but I want a clean one-liner without a temporary variable.
The shell substring construct you're using (:0:7) only works on variables, not command substitution. If you want to do this in a single operation, you'll need to trim the string as part of the pipeline, something like one of these:
echo "$(qstat -a | grep kig | sed 's/[.].*//')"
echo "$(qstat -a | awk -F. '/kig/ {print $1}')"
echo "$(qstat -a | awk '/kig/ {print substr($0, 1, 7)}')"
(Note that the first two print everything before the first ".", while the last prints the first 7 characters.) I don't know that any of them are particularly cleaner, but they do it without a temp variable...
qstat -u palle | cut -f 1 -d "." | xargs qdel
Kills all my jobs... normally I grep out the jobname(s) before cut'ing...
So I use a small script "idlist":
qstat -u palle | grep -E "*.in" | grep -E "$1" | cut -f 1 -d "." | xargs
To see all my "map_..." jobs:
idlist "map_*"
For killing all my "map_...." jobs:
idlist "map_*" | xargs qdel
yet another ways :
foreach m1 in $(qstat -a );do
if [[ $m1 =~ kig ]];then
m2=${m1%.kig}
echo "kig found $m2 "
break
fi
done

bash line concatenation during variable interpolation

$ cat isbndb.sample | wc -l
13
$ var=$(cat isbndb.sample); echo $var | wc -l
1
Why is the newline character missing when I assign the string to the variable? How can I keep the newline character from being converted into a space?
I am using bash.
You have to quote the variable to preserve the newlines.
$ var=$(cat isbndb.sample); echo "$var" | wc -l
And cat is unnecessary in both cases:
$ wc -l < isbndb.sample
$ var=$(< isbndb.sample); echo "$var" | wc -l
Edit:
Bash normally strips extra trailing newlines from a file when it assigns its contents to a variable. You have to resort to some tricks to preserve them. Try this:
IFS='' read -d '' var < isbndb.sample; echo "$var" | wc -l
Setting IFS to null prevents the file from being split on the newlines and setting the delimiter for read to null makes it accept the file until the end of file.
var=($(< file))
echo ${#var[#]}

Resources