Multiple variables are not getting printed in same line in shell script [duplicate] - linux

This question already has answers here:
How to echo "$x_$y" in Bash script?
(4 answers)
Closed 2 years ago.
Please see the below shell script:
#!/bin/bash
echo "Enter the date in (YYYY-MM-DD) format: "
read dt
i=00
echo "/opt/log-$dt_$i"
Expected Output :
Enter the date in (YYYY-MM-DD) format:
2020-06-18
/opt/log-2020-06-18_00
But Getting the below output:
Enter the date in (YYYY-MM-DD) format:
2020-06-18
/opt/log-00
Please suggest?

You need to change your echo line to this:
echo "/opt/log-${dt}_${i}"
Explanation:
An _ (underscore) is a valid character in a variable name, so bash is looking for $dt_ and $i. Since $dt_ is not defined, it doesn't print it. Bash provides the alternate variable syntax using ${} to explicitly isolate the variables when performing string interpolation like this.

Like this:
echo "/opt/log-${dt}_$i"
#  ^ ^
#  curly brackets mandatory here
This is because you have to separate the variables with _ that could be part of any variables.
Which editor do you use ? For me in vim, it's clear:
Versus:
(the underscore become white)

Related

Why is bash string comparator only working when given substring? [duplicate]

This question already has answers here:
Case insensitive comparison of strings in shell script
(14 answers)
Closed 1 year ago.
I am trying to add users from a csv file to a group in a bash script running on CentOS 8. The group names are "Faculty" and "Students", which I am forcing them to be lowercase. The following did not work. It defaults to the "else" clause, even when $groupName is "Faculty" (I would "echo" before the if statement).
if [ "$groupName" = "Faculty" ]
then
goodGroup="faculty"
else
goodGroup="student"
fi
However, it worked when I gave it a substring of only the capital letter:
if [ "${groupName:0:1}" = "F" ]
then
goodGroup="faculty"
else
goodGroup="student"
fi
Using the second method gives me the outcome I need, I am just curious why the first bit of code did NOT work. All the answers I've seen on StackOverflow say that's the syntax for comparing strings, so I can't see what I'm doing wrong.
Ways to force a variables value to lowercase in bash without having to check for specific values:
#!/usr/bin/env bash
# Using declare to give a variable the lowercase attribute
declare -l groupName1
groupName1=Faculty
printf "%s\n" "$groupName1"
# Using parameter expansion
groupName2=FACULTY
printf "%s\n" "${groupName2,,}" # or "${groupName2#L}"

Why the assignment of an array string (with brackets) to environment variable is not working [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 2 years ago.
Execute the following command in bash shell:
export sz1='"authorities" : ["uaa.resource"]'
Now, try echo $sz1
I expect to see the following output:
"authorities" : ["uaa.resource"]
But instead I get this:
"authorities" : c
The interesting thing is that I have dozens of servers where I can execute this type of variable assignment and it works except on this server. This server has exactly the same OS version, profile, bash version etc. What could be the reason for this behavior?
Always quote your variables. Use
echo "$sz1"
When you don't quote the variable, word splitting and wildcard expansion is done on the variable expansion. On ["uaa.resource"] is a wildcard that will match any of the following filenames:
"
u
a
.
r
e
s
o
u
c
On that one machine you have a file named c, so the wildcard matches and gets replaced with that filename.

Bash execute string as command without expanding escaped spaces [duplicate]

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
How can I store a command in a variable in a shell script?
(12 answers)
Closed 2 years ago.
I have an external executable that I need to pass arguments to. With a bash script, I have code that determines these arguments. Some arguments may have escaped spaces.
I need to then execute that string, without expanding each of the arguments.
# ... some code that determines argument string
# the following is an example of the string
ARGSTR='./executable test\ file.txt arg2=true'
exec ${ARGSTR}
I must have the $ARGSTR be expanded so that I can pass arguments to ./executable, but each of the arguments should not be expanded. I have tried quoting "test file.txt", but this still does not pass it as one argument to ./executable.
Is there a way to do something like this?
Us an array instead of a string:
#!/usr/bin/env bash
ARGSTR=('./executable' 'test file.txt' 'arg2=true')
exec "${ARGSTR[#]}"
See:
BashFAQ-50 - I'm trying to put a command in a variable, but the complex cases always fail.
https://stackoverflow.com/a/44055875/7939871
This may achieve what you wanted :
ARGSTR='./executable test\ file.txt arg2=true'
exec bash -c "exec ${ARGSTR}"

Shell script won't run properly when re-assigning variable [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Variable variable assignment error -"command not found"
(1 answer)
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 4 years ago.
So I've got a shell script to do some lazy stuff for if the directory isn't changing for a user. It's below. Essentially, it should be an if statement that if the user enters "default" for the directory, it'll pull them to the default directory for the files. However, I'm getting a command not found on line 16, which is the reassignment statement.
The entire if statement below:
if [ $directory = "default" ];
then
echo Enter your ldap:
read $ldap
$directory = "/usr/local/home/google/${ldap}/Downloads"
fi
I've tried doing it without the dollar sign too...nothing. What's going on here? New to shell, couldn't find this question asked before either.
This is how you should assign a value to a variable in shell:
directory="/usr/local/home/google/${ldap}/Downloads"
No dollar ($) sign.
No space around equal (=) sign.
Also, you should wrap your variables inside double quotes ("). This way, you avoid errors with undefined variables, arguments with spaces, etc.
That gives us:
if [ "$directory" = "default" ]
then
echo "Enter your ldap:"
read $ldap
directory="/usr/local/home/google/${ldap}/Downloads"
fi

Change value of variable insead loop Bash [duplicate]

This question already has answers here:
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 4 years ago.
I'm newer on bash scripting ,I have a global variable that I want to change his value insead a loop in my script but still get an error that commande not found
this my script :
SCRIPT_BASE = "/home/scripts/test-Scripts"
CURRENT_SCRIPT_PATH = ""
declare -a arr=("A" "B" "C" "D")
for i in "${arr[#]}"
do
if [ $i == "A" ]; then
CURRENT_SCRIPT_PATH = $SCRIPT_BASE
echo -e "Current Path : $CURRENT_SCRIPT_PATH"
fi
done
when I run this script I get that CURRENT_SCRIPT_PATH commande not found
Thanks in advance for any help
In bash you should be really cautious about spaces in if conditions but also when you assign a value to a variable.
Replace in your code the following tree lines:
SCRIPT_BASE="/home/scripts/test-Scripts"
CURRENT_SCRIPT_PATH=""
CURRENT_SCRIPT_PATH=$SCRIPT_BASE
If you keep a space after the variable name bash will interpret it as a command and as you do not have commands SCRIPT_BASE, CURRENT_SCRIPT_PATH, CURRENT_SCRIPT_PATH in your current $PATH you have the error command not found that is produced.

Resources