You are not logged in.
I've been working on a bash script, and I'm trying to get it to move all directories that are not named certain names to another directory.
EDIT: Fixed the thing papajoke pointed out
#!/bin/bash
mv ~/Downloads/!(folders|pics|docs|code|archives|vids|sounds)/ ~/Downloads/folders/
The command from the script does what I want it to do when I run it from a terminal.
It also works if I run the script as follows:
source script.sh
It doesn't work like this:
bash script.sh
I'm trying to get it to run when I login, and using the running the command with "source" in my MATE Startup Applications doesn't work.
I'm new to bash scripting, any help would be much appreciated. Thanks
Last edited by physicsshark (2015-04-07 20:09:07)
"The Guide says that there is an art to flying," said Ford, "or rather a knack. The knack lies in learning how to throw yourself at the ground and miss." - Life, the Universe, and Everything
Offline
you have after sounds } for close but .... never open by {
Last edited by papajoke (2015-04-07 00:33:13)
lts - zsh - Kde - Intel Core i3 - 6Go RAM - GeForce 405 video-nouveau
Offline
you have after sounds } for close but .... never open by {
Hmmm.. thanks for pointing that out. I fixed that, but it still returns the same error and doesn't work.
"The Guide says that there is an art to flying," said Ford, "or rather a knack. The knack lies in learning how to throw yourself at the ground and miss." - Life, the Universe, and Everything
Offline
I've never seen that syntax, and it certainly doesn't work here. Are you running bash as your interactive shell, or something else?
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
I've never seen that syntax
You mean the pipes or the bang?
$ touch a.gz b.gz c.txt
$ ls !(*.gz)
c.txt
This works as an alias and from the comeliness commandline, but not in a script.
$ ls -l ~/2
total 16
drwxr-xr-x 2 karol users 4096 Apr 7 03:13 a
drwxr-xr-x 2 karol users 4096 Apr 7 03:07 b
drwxr-xr-x 2 karol users 4096 Apr 7 03:07 c
drwxr-xr-x 2 karol users 4096 Apr 7 03:13 d
$ mv ~/2/!(a|b|c) ~/2/a
$ ls -l ~/2
total 12
drwxr-xr-x 3 karol users 4096 Apr 7 03:14 a
drwxr-xr-x 2 karol users 4096 Apr 7 03:07 b
drwxr-xr-x 2 karol users 4096 Apr 7 03:07 c
$ ls -l ~/2/a
total 4
drwxr-xr-x 2 karol users 4096 Apr 7 03:13 d
Last edited by karol (2015-04-07 01:20:14)
Offline
I've never seen that syntax, and it certainly doesn't work here. Are you running bash as your interactive shell, or something else?
I'm running bash. I'm using the syntax described here.
It works from the terminal, just not from a bash script (as described in my post).
If you think there's a better way to do what I'm trying to do please let me know.
"The Guide says that there is an art to flying," said Ford, "or rather a knack. The knack lies in learning how to throw yourself at the ground and miss." - Life, the Universe, and Everything
Offline
I've never seen the bang used like that nor the expectation that regular expressions would be exanded on the command line in bash:
$ touch a.gz b.gz c.txt
$ ls !(*.gz)
bash: !: event not found
$ ls (*.gz)
bash: syntax error near unexpected token `('
EDIT: crossposted with the above, it seems this is the relevant difference:
shopt -s extglob
I suspect you need to set that in the script too as the script doesn't source your bashrc/bash_profile where you set that for your interactive session.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
This works as an alias and from the comeliness, but not in a script.
Well, it does work from my script if I run it using "source" instead of "bash". At this point, I'm confused.
"The Guide says that there is an art to flying," said Ford, "or rather a knack. The knack lies in learning how to throw yourself at the ground and miss." - Life, the Universe, and Everything
Offline
Thanks for the link.
Add 'shopt -s extglob' to the script to make it work.
Offline
with mv ?
but with find this syntax is ok
for exemple : find mydir ! -name "*.jpg" -type f -exec cp --parents {} mydest \;
lts - zsh - Kde - Intel Core i3 - 6Go RAM - GeForce 405 video-nouveau
Offline
Thanks for the link.
Add 'shopt -s extglob' to the script to make it work.
Okay, it works using bash now (from the command line), but it doesn't do what I want it to do when I run it from the following command in MATE's Startup Applications:
bash ~/.bashstuff/dd.sh
"The Guide says that there is an art to flying," said Ford, "or rather a knack. The knack lies in learning how to throw yourself at the ground and miss." - Life, the Universe, and Everything
Offline
with mv ?
but with find this syntax is ok
for exemple : find mydir ! -name "*.jpg" -type f -exec cp --parents {} mydest \;
Maybe 'find' doesn't use the bash extended globbing mechanism, but its own regexp etc.
Last edited by karol (2015-04-07 01:25:02)
Offline
Okay, it works using bash now (from the command line), but it doesn't do what I want it to do when I run it from the following command in MATE's Startup Applications:
bash ~/.bashstuff/dd.sh
This looks ridiculous, but this works:
bash -c "bash ~/.bashstuff/dd.sh"
Unless some one can provide a saner looking way to do this, I could mark this as "Solved".
Thanks everyone.
"The Guide says that there is an art to flying," said Ford, "or rather a knack. The knack lies in learning how to throw yourself at the ground and miss." - Life, the Universe, and Everything
Offline
Does
bash -c ~/.bashstuff/dd.sh
work?
Offline
physicsshark wrote:Okay, it works using bash now (from the command line), but it doesn't do what I want it to do when I run it from the following command in MATE's Startup Applications:
bash ~/.bashstuff/dd.sh
This looks ridiculous, but this works:
bash -c "bash ~/.bashstuff/dd.sh"
Because you need bash to expand the tilde for you. Specify the full path (or figure out if there's some alternative to ~ that you can use).
Last edited by Raynman (2015-04-07 18:35:10)
Offline
Does
bash -c ~/.bashstuff/dd.sh
work?
It does work.
I don't know why I didn't try that first. I assumed that bash -c was the equivalent of getting yourself to a command line,
but then you also needed to call bash (again) to run the script.
If my script evolves into something useful, I'll share it with the community.
Thanks! I'm going to mark this as solved.
EDIT: Community, community, community
Last edited by physicsshark (2015-04-07 20:10:21)
"The Guide says that there is an art to flying," said Ford, "or rather a knack. The knack lies in learning how to throw yourself at the ground and miss." - Life, the Universe, and Everything
Offline
karol wrote:Does
bash -c ~/.bashstuff/dd.sh
work?
It does work.
But then I think you're still running bash twice? First the command (in the above quote) passes -c and ~/.bashstuff/dd.sh to a bash process. That bash process expand the tilde and exec*()s the (executable) file. The shebang is read and a bash process is started to run the file. So it may look better, but it does pretty much the same as the uglier
bash -c "bash ~/.bashstuff/dd.sh"
Offline
But then I think you're still running bash twice?
Yes. This all seems very odd. I'm not sure why methods of starting bash twice are being compared to starting bash 3 times. Just start it once by putting the full path to the script itself into the startup applications. Scripts are made to be executable themselves (once chmod'ed). And as long as the setenv (edit: shopt) setting is in the script, then the original concerns of this thread should be addressed.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline