How to get length of variable bash? [duplicate] - linux

This question already has answers here:
Length of string in bash
(11 answers)
Closed 6 years ago.
I use Cent OS 7, and have written a Bash script.
I tried to get the length of variable:
#!/bin/bash
URL_STRING="1";
VAR_LENGTH=${#URL_STRING}
echo $VAR_LENGTH;
But I get syntax error

Try this code
echo $URL_STRING| awk '{printf("%d",length($0))}' | read asd

Related

Using grep number as variable is not working [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 months ago.
I want to grep a number of file in the current directory on linux.
E = $( grep -F "TOTEN" OUTCAR | tail -1 | awk '{printf "%12.9f \n",$5}')
When I run the script, it keeps telling me the command is not found.
I tried many ways to solve it by searching online but it did't work.

Output exactly x number of characters to variable using read in Bash [duplicate]

This question already has answers here:
Extract substring in Bash
(26 answers)
Closed 6 years ago.
How to retrieve the first 10 characters of a variable with Bash?
FOO="qwertzuiopasdfghjklyxcvbnm"
I need to get qwertzuiop.
If the variable is: FOO="qwertzuiopasdfghjklyxcvbnm"
then
echo ${FOO:0:10}
will give the first 10 characters.
Use the head command.
echo $FOO | head -c 10
=> qwertzuiop

How to set if condition in bash script? [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
bash, command not found [duplicate]
(2 answers)
Getting "command not found" error while comparing two strings in Bash
(4 answers)
Closed 2 years ago.
so i am new to bash scripting and trying to setup my first conditional block. I am using a shell script to get a JSON response and when the returned item is 0 i want it to do something.
QUEUE_SIZE=$(curl url | jq '.items | length')
if ["$QUEUE_SIZE" -eq "0"]
then
echo "Hey this is cool!!"
fi
When i run the above script, i get the following error: line 3: [0: command not found
Can someone please correct what i'm doing wrong? Thank you for the assistance.
Minor revision:
#!/bin/bash
QUEUE_SIZE=$(curl url | jq '.items | length')
if [ "$QUEUE_SIZE" == "0" ]; then
echo "Hey this is cool!!"
fi

Bash how to store in a variable the result of a linux command? [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 2 years ago.
I'm trying to store in a variable the temperature of the computer. I tried this but it doesn't work:
#!/bin/bash
temp = cat "/sys/class/thermal/thermal_zone0/temp"
echo "$temp"
i tried this too:
#!/bin/bash
temp = $(cat "/sys/class/thermal/thermal_zone0/temp")
echo "$temp"
but nothing works, it always says
./temp.sh: line 2: temp: command not found
Spaces are crucial! This works fine:
# NO space around `=`
temp=$(cat "/sys/class/thermal/thermal_zone0/temp")
echo "$temp"

Shell script variable value not getting update [duplicate]

This question already has answers here:
What is indirect expansion? What does ${!var*} mean?
(6 answers)
Dynamic variable names in Bash
(19 answers)
Is it possible to build variable names from other variables in bash? [duplicate]
(7 answers)
Closed 5 years ago.
I'm trying to execute below bash shell script, but not getting the expected output. Possible i'm doing something wrong or it's not the way of doing this.
#bin/bash
#set -x
path1_one=/home/dell/scripts
echo $path1_one
param_val=path1_one
param1=$( echo "$param_val" | awk -F '_' '{ print $0 }' )
#path2="$path1"
echo $param1
#echo $path2
Output:
/home/dell/scripts
path1_one
Expected Output:
/home/dell/scripts
/home/dell/scripts
Both variable value should be same,but don't know why param1 value is not reflecting with path1_one
You need to tell the script that you want to use the value of the variable path1, not the name path1.
Use:
path2="$path1"

Resources