Move files with exception in bash [duplicate] - linux

This question already has answers here:
Move all files except one
(14 answers)
Closed 3 years ago.
It works fine when I wrote this code on bash shell
mv -v `pwd`/!(.git) `pwd`/NewDir
But if I create shell script file like below,(name of this file is "s.sh")
#!/bin/bash
mv -v `pwd`/!(.git) `pwd`/NewDir
it returns error
./s.sh: line 2: syntax error near unexpected token `('
./s.sh: line 2: `mv -v `pwd`/(!.git) `pwd`/NewDir'
How can I fix it?

!(.git) is an extended glob, you need to enable extglob to make it work in a non-interactive shell. And I think instead of calling pwd twice, you can use PWD variable in this case.
shopt -s extglob
mv -v "$PWD"/!(.git) "$PWD/NewDir"

Related

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

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.

How to replace path stored in variable using sed [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 12 months ago.
I've a script
#!/bin/bash
mv /home/nnice/Downloads/Images/* /run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/Images
mv /home/nnice/Downloads/Video/* /run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/Videos
I need to replace path using sed command. I've stored paths in variables
replace="/run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/"
replacewith="/home/nnice/Windows/D/FILES/Softwares/HTTP/Downloads/"
I am trying following command but it doesn't work
sed -i "s/$replace/$replacewith/g" script.sh
I've also used different separators instead of / but script remains unchanged.
[nnice#myhost scripts]$ sed "s|$replace|$replacewith|g" script.sh
#!/bin/bash
mv /home/nnice/Downloads/Images/* /run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/Images
mv /home/nnice/Downloads/Video/* /run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/Videos
can you please help me with that to replace them using sed command?
Thank you
Your command fails because you're using the same separator for the sed command and your file paths. File paths need to use / but sed separators can be anything, so try this:
sed -i "s#$replace#$replacewith#g" script.sh

find: missing argument to `-exec' when running from script file [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 3 years ago.
When I run the following command from my script file it gives me:
find: missing argument to `-exec'
But the same command from the command line works normally:
find /home/poseidoncharters/poseidon_backup/*.sql -mtime +1 -exec rm -f {} \;
I run the script like ./myscript.sh
Well as other said on their comment command seems correct, and it was. the problem i was having was newline character problem, as i wrote this script in windows machine and then uploaded to unix server, so as soon as i ran dos2unix remove_backup.sh it started to work.

syntax error: "(" unexpected when sh a file [duplicate]

This question already has answers here:
Why does process substitution not work in a shell script?
(1 answer)
Bash script process substitution Syntax error: "(" unexpected
(3 answers)
Closed 5 years ago.
i running a file containning such a command
comm -3 <(cut -f 1 -d' ' <./atoz |sort) <(cut -f 1 -d' '
It succeeds when it was run outside of the file.
But i get error:"file1: 1: file1:Syntax error: "(" unexpected ", when i type
sh file1 file2 and try to run the command from the file1.
The file have no any #!/bin/bash or .sh suffix since i dont have the background to solve this kind of problem..
Does somebody know how to solve it? Thanks a lot
Process substitution (<()) is a bash feature. It is not supported with /bin/sh, which guarantees only features present in the POSIX sh specification (on platforms conformant with 1992-era or newer POSIX specifications; on old ones, it could be 1970s-era Bourne).
Use bash yourscript, or a #!/bin/bash shebang, to run this file.

Capturing command output in a shell variable isn't working [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 5 years ago.
I am new to shell scripting, and need to output a series of commands to a local variable in a shell script, but keep on failing. For instance, the output of grep -c to a variable that will be use in an if statement. If anyone can redirect me over to a source that explains the process, I will appreciate.
#!/bash/sh
myVar = ls ~/bin | grep -c $0
Posting your code at shellcheck.net gives you valuable pointers quickly:
myVar = ls ~/bin | grep -c $0
^-- SC2037: To assign the output of a command, use var=$(cmd) .
^-- SC1068: Don't put spaces around the = in assignments.
^-- SC2086: Double quote to prevent globbing and word splitting.
If we implement these pointers:
myVar=$(ls ~/bin | grep -c "$0")
Also note that your shebang line has an incorrect path - the #! must be followed by the full path to the executing shell's binary.
Resources for learning bash, the most widely used POSIX-compatible shell:
Introduction: http://www.faqs.org/docs/Linux-HOWTO/Bash-Prog-Intro-HOWTO.html
Guide: http://mywiki.wooledge.org/BashGuide
Cheat sheet: http://mywiki.wooledge.org/BashSheet

Resources