Creating bash variables from a files in a folder [duplicate] - linux

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 7 months ago.
I have a bash script (list.sh) that lists files in a folder and assigns each filename to a bash variable:
#/bin/bash
c=0
for file in *; do
varr$c="$file";
c=$((c+1));
done
When I call this from a bash terminal with:
source list.sh
I get:
bash: varr0=07 Get the Balance Right!.mp3: command not found...
bash: varr1=190731_10150450783260347_1948451_n.jpg: command not found...
bash: varr2=199828_10150450907505347_7125763_n.jpg: command not found...
bash: varr3=2022-07-31_19-30.png: command not found...
bash: varr4=2022-08-02_12-06.png: command not found...
bash: varr5=246915_10152020928305567_1284271814_n.jpg: command not found...
I don't know how to put quotes around the file text itself, so that each varr(x) creates itself as a variable in the parent bash script, ie:
varr0="07 Get the Balance Right!.mp3"

It's not a "quote around the text" issue, it's the variable declaration varr$c that's not working. You should be using declare instead.
This script solves your problem :
#/bin/bash
c=0
for file in *; do
declare varr$c=$file;
c=$((c+1));
done

You can use the keyword declare as in
$ n=1
$ declare var_$n=20
$ echo $var_1
20
https://www.linuxshelltips.com/declare-command-in-linux/

Try this script:
c=0
for file in *; do
printf -v var$((c++)) '%s' "$file"
done
# list variables starting with var and their values
for v in ${!var*}; do
printf '%s=%s\n' "$v" "${!v}"
done
Though using an array must have been preferred over this method.

Related

How to concatenate multiple shell variables into one?

I need to form a single variable from a for loop.
My script:
#! /bin/sh
if [ "$#" -le 1 ]; then
echo "Illegal number of parameters"
exit 1
else
# Form domains variable
DOMAINS=""
for i in "${#:2}"; do
DOMAINS+="$i,"
done
echo "${DOMAINS::-1}"
fi
When I execute it:
sh script.sh command domain1 domain2
I get the following error:
certbot.sh: line 10: DOMAINS+=mmand,: not found
certbot.sh: line 10: DOMAINS+=domain1.com,: not found
certbot.sh: line 10: DOMAINS+=domain2.com,: not found
It seems as I used bash syntax since the following execution works:
bash script.sh command domain1.com domain2.com
I get:
domain1.com,domain2.com
I need it to work as sh not bash. I can't seem to find a solution.
Just:
IFS=,
echo "$*"
Or you seem to want from a second argument. Then like:
( shift; IFS=,; echo "$*" )
+= is not valid in a POSIX shell.
Since it is not a valid variable assignment,
DOMAINS+="$i,"
is interpreted as the name of a command, which is obtained by parameter expansion of i. For instance, if i equals 1, the line corresponds to
DOMAINS+=1,
If you had an executable file named DOMAINS+=1, in your PATH, this file would be run.
You have to catenatate variables like this:
FOO=$FOO$BAR$BAZ
You can't avoid repeating the name FOO.
An alternative would be to switch to zsh or bash, where your usage of += would indeed have the desired effect.
Did you try changing DOMAINS+="$i," into DOMAINS="${DOMAINS}${i}," relying on variable substitution?

how to copy the bash file itself to the output dir [duplicate]

This question already has answers here:
How do I know the script file name in a Bash script?
(25 answers)
Closed 2 years ago.
I want to save a copy of the bash file each time I run it. It should be saved to the output dictionary.
I am doing it like this in the mytrainrtest.sh file:
mkdir -p "${EXP_DIR}/train"
cp "${WORK_DIR}"/mytrainrtest.sh "${EXP_DIR}"/.
Now I have much more bash files with name my****** as copies of the upper one, each with different names.
How can I write the line, so the bash file will recognize its name to copy itself?
Use the $0 special variable which contains the name of the currently executing script.
cp "$0" "$exp_dir"/
Script name(path) stored in a special var $0
#!/bin/bash
echo $0
$ ./test
./test

Passing argument into shell script as a form of txt file

I would like to know how to access the contents of a variety of txt files by passing arguments into shell scripts. I'll have different files and I'm expecting to execute with this command:
./script.sh FileA.txt
What should I put into my shell script so that I can access and manipulate the contents of the files?
I tried this but it outputs 0:
echo "$#"
I also tried these, but both output nothing:
for i in $1
do
echo "$i"
done
echo "$1"
To sum up the contents see this link to understand bash arguments more https://tecadmin.net/tutorial/bash-scripting/bash-command-arguments/ . Also as #Barmar said, to iterate through a list of arguments of unknown quantity use for i in "$#" .
edit
and as #Barmar said, $1 is simply the name of the argument. So echoing $1 will just echo the name.
I don't understand your question fully. Lets assume you have list of file names in a text file "FileA.txt".
And you wanted to run some commands for each file in the "FileA.txt" file.
Can you try below:
for i in `cat $1`
do
echo $i
done

File read not interpreting Environment variables [duplicate]

This question already has an answer here:
Expand ENV variable of a string, run command and store in variable?
(1 answer)
Closed 6 years ago.
When I try to read a file which contains a environment variable HOSTNAME.
It is not interpreting its value while reading the file.
For example, if my hostname is linux1.com.
When I try to read a sample file(Test.txt) below
/var/log/$HOSTNAME
using the code below
while read line
do
ls -l $line
done < Test.txt
I am expecting it to interpet the $HOSTNAME variable and print it. But it is not working. It is directly doing ls -l /var/log/$HOSTNAME, instead of
ls -l /var/log/linux1.com
The same command is intrepreting the hostname when I run this command in shell.
Any leads on this is highly appreicated.
Thanks.
Parameter expansion is not recursive. When $line is expanded, its value is subject to pathname expansion (globbing) and word-splitting, but not further parameter expansions.

How to call a variable as a path from another script?

Example
Var = '/etc/sysconfig/..'
export Var
bash script1.sh
in another script1
cat $Var
This is my Problem: The variable does not call the file in this path
Do this:
Var='/etc/sysconfig/..'
bash script1.sh "$Var"
Then in script1.sh:
Var=$1
cat "$Var"
The quotes around "$Var" are required to support paths containing spaces.
Your variable assignment is wrong, it should be:
Var='/etc/sysconfig/..'
No spaces around =.
If you want to send in a environment variable for one script only then you can use:
Var='/etc/sysconfig/..' ./my_script.sh
And inside my_script.sh:
printf "%s\n" "$Var"
# Will print /etc/sysconfig/..
If you want to send arguments to my_script.sh do what #JohnZwinck suggested. What I suggested is only to change environment variable and shouldn't be abused to send/receive regular variables to a command.
I think no need to to more thing
script 1
#!/bin/bash
a="/home/example" ### you can do with export command also export a="/home/example"
sctipt2 ## make effective
. script1;
cd $a

Resources