Change value of variable insead loop Bash [duplicate] - linux

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.

Related

How can we execute a string command and then get the result of it to assign a variable in shell script bash [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 12 months ago.
I want to execute a string command and then get the result of it to assign a variable in shell script bash.
for example :
strCode="scontrol show jodid --dd $VALUE"
eval $strCode
The point here is this codepart worked but how can I get the results from the `
eval
` command and assign it to a variable, because I need it to use.
when I type the theResult=eval $strCode it does not work.
thanks for the help
The solution is:
strCode=$(scontrol show jodid --dd $VALUE)
echo "${strCode}"
It's easier than you are doing:
theResult=$(scontrol show jodid --dd $VALUE)
You should follow some bash tutorial, as this is a quite basic question.

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.

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

Linux batch: create multi folder by combine name with serial number [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
I'm try to create multi folder by combine string and counter. I don't why what is the wrong with my code:
echo 'Start'
let count=0
for p in {1..10}
do
DirName= "dir"
NUM = "${DirName}${count}"
let count++
mkdir $NUM
mkdir "$NUM"/decoded
done
I got this kind of error
./test.sh: line 6: dir: command not found
./test.sh: line 7: NUM: command not found
thank in advance
No need to use a loop here. The shell will do all the necessary expansion for you. In fact, you're already relying on the shell to expand {1..10} for you as part of your for loop. So you can just use that expansion directly with mkdir. Also by using mkdir -p <path> (make parent directories as needed), you can avoid having to first do mkdir $NUM before doing mkdir $NUM/decoded.
Putting it all together, you can do what you need in a single line:
mkdir -p dir{1..10}/decoded
Edit: To answer your question more directly regarding the command not found errors, it looks like (as Bjorn A. mentioned) you just need to get rid of the spaces before and after the = in your variable assignments.
You cannot have spaces around the assignment operator in bash. Lines 6 and 7 must look like:
DirName="dir"
NUM="${DirName}${count}"

Resources