Passing variable and evaluating string [duplicate] - string

This question already has answers here:
Lookup shell variables by name, indirectly [duplicate]
(5 answers)
How to get a variable value if variable name is stored as string?
(10 answers)
Bash - variable variables [duplicate]
(4 answers)
Closed 4 years ago.
I have a set of directories listed inside a text file as
DIR_A= (name of directory 1)
DIR_B= (name of directory 2)
....
I have a second script to which I would like to pass an argument like sh Scriptname varname where varname could be A, B, ...
Scriptname sources the initial text file and I would like this script to accept the passed varname (using $1) to echo DIR_varname.
Any ideas? TIA

Related

expand unix variable inside sed command [duplicate]

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
sed substitution with Bash variables
(6 answers)
Closed 4 years ago.
I need to replace current value in configuration file with new value which is assigned to variable ,
like
file_name=abc.txt
needs to be replaced like
file_name=xyz.txt
where $file=xyz.txt
I tried
sed -i 's/file_name=.*/file_name=$file/g' conf_file.conf
however the variable is not getting expanded,
it comes like file_name=$file.
any pointers?
This should work,assuming that variable file has value:xyz.txt assigned to it:
sed "s/file_name=.*/file_name=${file}/g" file_name
Output:
file_name=xyz.txt

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

Passing multiple arguments in Bash Script containing space [duplicate]

This question already has answers here:
Iterate over a list of files with spaces
(12 answers)
How to iterate over arguments in a Bash script
(9 answers)
Closed 5 years ago.
I just started learning Bash Script.
My script is accepting 1 to n number of arguments. Each argument is pass to a function rename.
My problem is that the argument I pass does not accept space.
script.sh
#!/bin/bash
for FILE in $#
do
echo "$FILE"
rename $FILE
done
For Ex:
./script.sh /Users/xyz/Section 5/abc/ /Users/xyz/pqr/ /Users/z/abc
The above argument "/Users/xyz/Section 5/abc/" should be one even if it contains space.
But the code in script.sh will break it into two argument.
So the output is:
/Users/xyz/Section
5/abc/
/Users/xyz/pqr/
/Users/z/abc
But My Expected Output should be:
/Users/xyz/Section 5/abc/
/Users/xyz/pqr/
/Users/z/abc
Note: Different solution I tried till now:
1) "/Users/xyz/Section 5/abc/" --> Same output ie 2 different argument
2) '/Users/xyz/Section 5/abc/' --> Same output ie 2 different argument
3) /Users/xyz/Section\ 5/abc/ --> Same output ie 2 different argument

Assign to dynamic variable name in Bash [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
how to use variable variable names in bash script [duplicate]
(1 answer)
Closed 6 years ago.
How do I assign a value to variable that has a variable in its name?
var1="file"
var2_$var1="folder"
The code above gives me the error -bash: var2_file=folder: command not found. I was curious to know how to assign to a variable with another variable in its name.
Version of Bash is "GNU bash, version 4.1.2"
With bash you can use declare:
declare var2_$var1="123"
How about using another variable to hold the dynamic name and use it for retrieving the value after setting?
new_var=var2_$var1
declare var2_$var1="123"
echo "${!new_var}" # => 123
Unfortunately, Bash doesn't allow declare $new_var="123" - that would have made this a little prettier.

Redirecting bash output to a path stored in a variable [duplicate]

This question already has an answer here:
Bash - Concatenating Variable on to Path
(1 answer)
Closed 7 years ago.
I'm trying to redirect my output to a file which has its path stored in a variable but I cant get it to work.
LOG_TEST="~/AVDS/logs/${HOSTNAME}_testlog.log"
echo "foo" >> ~/AVDS/logs/${HOSTNAME}_testlog.log
echo "bar" >> $LOG_TEST
The "foo" line will work fine but the "bar" line returns the error:
./testarea.sh: line 9: ~/AVDS/logs/tvpc-office_testlog.log: No such file or directory
What am I doing wrong here?
Tilde expansion only happens when unquoted.
Get in the habit of using $HOME, not ~, in scripts.

Resources