Loop through an array in Bash [duplicate] - bash

This question already has answers here:
Loop through an array of strings in Bash?
(21 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 years ago.
I'm trying to loop through an array in Bash when using rclone. I have set up an array of paths, and I'm looping through them, but I also want to echo the path name in the console window. When I run my code, because the paths have spaces in them, the echo command shows each part of the path, instead of the whole path. Here is my code;
folder[0]="a path here"
folder[1]="another path here"
folder[2]="another path here"
folder[3]="another path here"
for item in ${folder[*]}; do
echo "syncing $item ..."
rclone copy ~/"Documents/$item" Box:"$item" -u -vv --syslog
done
What I see is
syncing 'a' ...
syncing 'path' ...
syncing 'here' ...
syncing 'another' ...
syncing 'path' ...
syncing 'here' ...
What I want to see is the full path string. Not sure what I'm doing wrong here, I guess it's the referencing in the folder[*] causing me the problem.

Related

when file name has space, how to use variable in script? [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 10 months ago.
I want to open a application using shell script.
But! when file name has space, script read this as two files.
1 path=/Applications
2 program=/Google Chrome.app
3 open $path$program
it's not working...
when I use alias like these...
program="/Google Chrome.app" or
program=/Google\ Chrome.app or
program="/Google\ Chrome.app"
shell cognized two files like this
"The files /Applications/Google and /current_dir/Chrome.app do not exist."
How I fix that?
please give me your teaching 🧐
You can wrap your variables in quotes. So what you can do is:
path=Applications
program="Google Chrome.app"
open /$path/$program

Git log <since> and <before> script not able to execute with space in var [duplicate]

This question already has answers here:
bash script execute command with double quotes, single quotes and spaces
(2 answers)
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Closed 1 year ago.
I have a script to output git commits with specific jira ticket and within a date and time range, however i keep hitting an error, my codes are below:
SINCE="2021-02-26 17:59:58"
BEFORE="2021-03-05 17:59:58"
CMD="git log -p -m -name-status --since=\"${SINCE}\" --before=\"${BEFORE}\" --grep=${TICKET}
${CMD} >> out.txt
Error:
Fatal: Invalid object name '17'
I tried enclosing the cmd with "" "${CMD}" also getting same error.
Anyone know what is wrong with it?

bash cp filenames with spaces [duplicate]

This question already has answers here:
Tilde in path doesn't expand to home directory
(5 answers)
Closed 4 years ago.
I'm using variables in bash script to hold folder names (to iterate over multiple folders).
I'd like to copy files from one to another, the files exist in source directory. Folders and filenames contain spaces, so I must use double quote.
For instance:
#!/bin/bash
inpath="~/foo bar/"
outpath="~/temp basket/
cp "$inpath*" "$outpath"
The copy fails as: '~/foo bar/*' No such file or directory
Is there any consistent way to do that?
Only quote the parts you don't want expanded or split:
inpath=~/'foo bar'
outpath=~/'temp basket'
cp -- "$inpath/"* "$outpath"

I want to move all files from one folder to other folder using shell script [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 4 years ago.
I want to move all files from one folder to other folder using shell script.
This is my code but it throws error
#!/bin/sh
SRC = '/home/xxx/test1/'
DESTN = '/home/xxx/test/'
mv SRC DESTN
Error:./move.sh:2:./move.sh:SRC:not found
./move.sh:2:./move.sh:SRC:not found
mv:cannot stat 'SRC': No such file or directory
When declaring shell variables, you cannot add spaces between the variable name and the = sign, nor between the = and the value.
Also remember to add $ before the variable name when using it after its declaration.
Your script should look like this one:
#!/bin/sh
SRC="/home/xxx/test1/*"
DESTN="/home/xxx/test"
mv "$SRC" "$DESTN"

Wrong bash script syntax [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 7 years ago.
I have a script part where I am checking if the file exist . I dont find where is my mistake because it jumps to else( file exists) and starts downloading that file. Part of code:
...
if [ -f '$ins.img' ];
then
echo '$ins.img already exists'
else
wget http://$2/$ins/$ins.img
fi
...
It's because you're using single quotes in your if. Variables are not filled in inside single quotes, so it will check for a file literally named $ins.img, which obviously doesn't exist.
The solution is easy, use double quotes.

Resources