How to use variables in brace expansion [duplicate] - linux

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 1 year ago.
This works as expected:
$ echo file-{00..03}
file-00 file-01 file-02 file-03
This does not do what I wanted:
$ start=00
$ end=03
$ echo file-{$start..$end}
file-{00..03}
The reason is that brace expansion is performed before any other epansion.
In my case, the sequence limits are in the variables start and end.
Any clever way to hack my way around this?

I have this:
$ eval echo file-{$start..$end}
file-00 file-01 file-02 file-03
But I am open to less ugly suggestions.

Related

bash string manipulation got errors in jenkins shell (not pipeline) [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 3 years ago.
i am trying to get value of variable by given string:
Running this code on Jenkins and its get "bad substitution"
in regular shell it works.
example:
param1="hello"
param2="world"
PARAMS="param1 param2"
for p in $PARAMS;do
echo ${!p}" "
done
what the best way to make it work in Jenkins too.
You must use with $ sign before the name of the variable:
PARAMS="$param1 $param2"
for p in $PARAMS;do
echo ${p}" "
done

How do I correctly read in a file with sh scripting and using it in an if statement? [duplicate]

This question already has answers here:
Assignment of variables with space after the (=) sign?
(4 answers)
Assing a variable and use it inside of if statement shell scripting
(2 answers)
Difference between sh and Bash
(11 answers)
Closed 3 years ago.
So there is a kernel adiutor for android, that let's you add custom controls with shell scripting. I'm trying to add a switch, but I have trouble setting the switch up correctly, when it's active, and when it's not. So there is a (text)file I'm trying to read (you will see it in the code), whether it's 0 or 1 inside, and that determines the switch on-off state.
I've tried with cat, read, everything, but honestly I think the problem is that I'm not familiar with sh scripting, and there is a problem with my syntax. Sometimes the script won't return anything when using cat. Also, su is available so that's not a problem, also the file has the correct permissions.
#!/system/bin/sh
var= $(<sys/class/lcd/panel/mdnie/hdr)
if ( "$var" = 0) then
echo 0
else echo 1
fi
The problem with my code is that right now it returns 1 (on), even when the file has a 0.
When assigning a variable in shell, there must be no space after the assignment sign. Also, make sure you use the correct syntax for conditions (and be aware of whitespace sensitivity):
var=$(cat sys/class/lcd/panel/mdnie/hdr)
if [ "$var" = "0" ]; then
# if [ "$var" -eq 0 ], if you want numeric comparison (won't really matter here)
echo 0
else
echo 1
fi

Is there any difference in how variables are referenced in shell script? [duplicate]

This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 3 years ago.
Consider the following code:
name=John
echo ${name}
It prints "John", just as expected. Now consider this code:
name=John
echo $name
Again, this code prints "John" just as expected. Both codes work fine.
But I wonder is there any difference between the two, e.g. compatibility?
In your case, there is no difference.
In this case, there is:
name=John
echo ${name}Doe
echo $nameDoe
Read more: here

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

Convert string to bash argument [duplicate]

This question already has an answer here:
Bash missing $(HOME) variable
(1 answer)
Closed 5 years ago.
i want to know if there is a way to do something like this
VAR="-o sampleoutput"
./mycommand.sh $(VAR)
Thank you
Edit: This question is different. When i tried to run my command withuot parenthesis too but it did not work. Here is my code
ADDITIONAL_ARGUMENTS="-J-javaagent:/opt/newrelic/newrelic.jar &"
./bin -Dconf=./configfile $ADDITIONAL_ARGUMENTS
This is my result
Bad root server path: /path/to/my/project/&
Remove your () around VAR.
VAR="-o sampleoutput"; ./mycommand.sh $VAR

Resources