Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #238168 > unrolled thread

Bash script problem

Started by"Gary L. Roach" <garyroach719@gmail.com>
First post2021-08-05 02:00 +0200
Last post2021-08-06 01:00 +0200
Articles 15 on this page of 35 — 13 participants

Back to article view | Back to linux.debian.user


Contents

  Bash script problem "Gary L. Roach" <garyroach719@gmail.com> - 2021-08-05 02:00 +0200
    Re: Bash script problem Greg Wooledge <greg@wooledge.org> - 2021-08-05 02:10 +0200
      Re: Bash script problem "Gary L. Roach" <garyroach719@gmail.com> - 2021-08-05 05:50 +0200
        Re: Bash script problem john doe <johndoe65534@mail.com> - 2021-08-05 09:00 +0200
        Re: Bash script problem <tomas@tuxteam.de> - 2021-08-05 10:10 +0200
          Re: Bash script problem Greg Wooledge <greg@wooledge.org> - 2021-08-05 13:20 +0200
            test vs. [ [was: Bash script problem] <tomas@tuxteam.de> - 2021-08-05 14:00 +0200
              Re: test vs. [ [was: Bash script problem] Greg Wooledge <greg@wooledge.org> - 2021-08-05 14:40 +0200
                Re: test vs. [ [was: Bash script problem] <tomas@tuxteam.de> - 2021-08-05 15:00 +0200
        Re: Bash script problem Greg Wooledge <greg@wooledge.org> - 2021-08-05 14:30 +0200
    Re: Bash script problem Anssi Saari <as@sci.fi> - 2021-08-05 09:40 +0200
      Re: Bash script problem Tixy <tixy@yxit.co.uk> - 2021-08-05 10:30 +0200
        Re: Bash script problem <tomas@tuxteam.de> - 2021-08-05 10:40 +0200
          Re: Bash script problem Greg Wooledge <greg@wooledge.org> - 2021-08-05 14:00 +0200
            Re: Bash script problem "Gary L. Roach" <garyroach719@gmail.com> - 2021-08-05 22:10 +0200
              Re: Bash script problem Greg Wooledge <greg@wooledge.org> - 2021-08-05 22:20 +0200
                Re: Bash script problem "Gary L. Roach" <garyroach719@gmail.com> - 2021-08-06 17:00 +0200
                  Re: Bash script problem <tomas@tuxteam.de> - 2021-08-06 17:10 +0200
              Re: Bash script problem David <bouncingcats@gmail.com> - 2021-08-06 01:40 +0200
                Re: Bash script problem Greg Wooledge <greg@wooledge.org> - 2021-08-06 02:10 +0200
                  Re: Bash script problem Polyna-Maude Racicot-Summerside <debian@polynamaude.com> - 2021-08-06 02:20 +0200
                    Re: Bash script problem Greg Wooledge <greg@wooledge.org> - 2021-08-06 02:30 +0200
                      Re: Bash script problem The Wanderer <wanderer@fastmail.fm> - 2021-08-06 03:50 +0200
                        Re: Bash script problem Greg Wooledge <greg@wooledge.org> - 2021-08-06 04:10 +0200
                Re: Bash script problem Polyna-Maude Racicot-Summerside <debian@polynamaude.com> - 2021-08-06 02:10 +0200
                  Re: Bash script problem Christian Groessler <chris@groessler.org> - 2021-08-06 02:30 +0200
                  Re: Bash script problem Christian Groessler <chris@groessler.org> - 2021-08-06 03:00 +0200
                    Re: Bash script problem Greg Wooledge <greg@wooledge.org> - 2021-08-06 03:10 +0200
                    Re: Bash script problem David <bouncingcats@gmail.com> - 2021-08-06 03:10 +0200
                  Re: Bash script problem David <bouncingcats@gmail.com> - 2021-08-06 03:00 +0200
                    Re: Bash script problem <tomas@tuxteam.de> - 2021-08-06 09:00 +0200
                Re: Bash script problem David Wright <deblis@lionunicorn.co.uk> - 2021-08-06 05:10 +0200
                  Re: Bash script problem David <bouncingcats@gmail.com> - 2021-08-06 05:50 +0200
                Re: Bash script problem Darac Marjal <mailinglist@darac.org.uk> - 2021-08-06 09:20 +0200
    Re: Bash script problem Tom Browder <tom.browder@gmail.com> - 2021-08-06 01:00 +0200

Page 2 of 2 — ← Prev page 1 [2]


#238231

FromPolyna-Maude Racicot-Summerside <debian@polynamaude.com>
Date2021-08-06 02:20 +0200
Message-ID<CIVKF-1it-3@gated-at.bofh.it>
In reply to#238229

[Multipart message — attachments visible in raw view] — view raw

Hi,

On 2021-08-05 8:07 p.m., Greg Wooledge wrote:
> On Thu, Aug 05, 2021 at 08:01:22PM -0400, Polyna-Maude Racicot-Summerside wrote:
>> find . -type d -exec chmod -v 0644 '{}' \;
>>
>> to change the folder
>>
>> find . -type f -exec chmod -v 0755 '{}' \;
>>
>> to change files
> 
> You've switched the permissions around.  You want 644 on the files, and
> 755 on the directories.
> 
Indeed, I changed permission between folders and directories.

> Use + instead of \; to make them more efficient.  Also, you don't need
> to quote the {} argument.  I've never been able to figure out where
The quote around {} come from the man page of chmod.
What does + make as a difference ?
> that tradition came from, because I've never seen *any* shell where
> it's needed.  But at least it's harmless.
> 

Thanks for adding those useful information :)
-- 
Polyna-Maude R.-Summerside
-Be smart, Be wise, Support opensource development

[toc] | [prev] | [next] | [standalone]


#238233

FromGreg Wooledge <greg@wooledge.org>
Date2021-08-06 02:30 +0200
Message-ID<CIVUl-1lo-3@gated-at.bofh.it>
In reply to#238231
> >> find . -type d -exec chmod -v 0644 '{}' \;

> > Use + instead of \; to make them more efficient.

> What does + make as a difference ?

It's a replacement for xargs, except that it actually works, unlike
xargs, which is horribly broken without GNU extensions.

find . -type d -exec chmod 755 {} +

is equivalent to

find . -type d -print0 | xargs -0 chmod 755

except that the former is (a) faster, (b) shorter, and (c) POSIX standard.

Both of them try to minimize the number of separate calls to chmod, by
bundling up a bunch of filenames and passing them all at once.

The \; command calls chmod one time for every directory.

There are valid reasons to use the \; form (e.g. when your command can
only handle one filename at a time), and there are valid reasons to
use the -print0 | xargs -0 form (e.g. when you want to parallelize,
which is another GNU xargs extension).  But in this particular case,
the -exec + command is the best choice.

[toc] | [prev] | [next] | [standalone]


#238238

FromThe Wanderer <wanderer@fastmail.fm>
Date2021-08-06 03:50 +0200
Message-ID<CIX9M-1YH-1@gated-at.bofh.it>
In reply to#238233

[Multipart message — attachments visible in raw view] — view raw

On 2021-08-05 at 20:25, Greg Wooledge wrote:

>> >> find . -type d -exec chmod -v 0644 '{}' \;
> 
>> > Use + instead of \; to make them more efficient.
> 
>> What does + make as a difference ?
> 
> It's a replacement for xargs, except that it actually works, unlike
> xargs, which is horribly broken without GNU extensions.
> 
> find . -type d -exec chmod 755 {} +
> 
> is equivalent to
> 
> find . -type d -print0 | xargs -0 chmod 755
> 
> except that the former is (a) faster, (b) shorter, and (c) POSIX standard.

I believe I've hit contexts in which I could use '-print0 | xargs -0'
but couldn't figure out a way to get the job done with '-exec', because
I needed the command which was being run to process the output to be a
pipeline. (I no longer remember the specifics of the tasks involved, so
I can't explain why this was what I needed.)

The only way I can/could think of to do that with an argument to '-exec'
involved putting the pipeline sequence into a script and calling that
via -exec, but that's more unwieldy than I want to work with in most cases.

-- 
   The Wanderer

The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all
progress depends on the unreasonable man.         -- George Bernard Shaw

[toc] | [prev] | [next] | [standalone]


#238239

FromGreg Wooledge <greg@wooledge.org>
Date2021-08-06 04:10 +0200
Message-ID<CIXt7-2kg-3@gated-at.bofh.it>
In reply to#238238
On Thu, Aug 05, 2021 at 09:47:04PM -0400, The Wanderer wrote:
> I believe I've hit contexts in which I could use '-print0 | xargs -0'
> but couldn't figure out a way to get the job done with '-exec', because
> I needed the command which was being run to process the output to be a
> pipeline. (I no longer remember the specifics of the tasks involved, so
> I can't explain why this was what I needed.)

I mentioned, in the part of the message you didn't quote, that there
are valid use cases for -print0 | xargs -0.  I only gave one specific
example, but I'm sure there are more.

"It's what I'm used to" is a fair reason, too.  Espcially when you're
writing scripts that you only need to use once.

> The only way I can/could think of to do that with an argument to '-exec'
> involved putting the pipeline sequence into a script and calling that
> via -exec, but that's more unwieldy than I want to work with in most cases.

One of the more advanced techniques looks like this:

find ... -exec bash -c '
  some commands
  go here
' x {} +

You can use this in cases where you might want to -exec a script,
but you don't want to create a second file.  It's not unreasonable
to write... say, about 10 lines of bash code in the indented section.
I'd try to avoid doing any more than that.  Just remember that you've
already used up your single quotes, so you can't put single quotes in
there (unless you write them as '\'').

For more discussion on this topic, please see

https://mywiki.wooledge.org/UsingFind
https://mywiki.wooledge.org/BashProgramming/03

[toc] | [prev] | [next] | [standalone]


#238230

FromPolyna-Maude Racicot-Summerside <debian@polynamaude.com>
Date2021-08-06 02:10 +0200
Message-ID<CIVAZ-1fl-3@gated-at.bofh.it>
In reply to#238228

[Multipart message — attachments visible in raw view] — view raw

Hi,

On 2021-08-05 7:30 p.m., David wrote:
> On Fri, 6 Aug 2021 at 06:03, Gary L. Roach <garyroach719@gmail.com> wrote:
> 
>> Second, why am I separating out the Path the way I am doing? I need to
>> check each level for existence then, if the level doesn't exist, create
>> the directory, cd to the directory, set chown and -x chmod. After that
>> check the next level and repeat the process until I run out of levels.
>> There are other things that need to be done with files that are similar.
> 
> It sounds like you are unaware of 'mkdir -p' and 'chown -R'.
> They are the standard tools for this kind of task.
> 
> 'chmod -R' is less useful because it does not discriminate
> between files and directories, I never understood why it
> does not offer that option, because usually we need all file
> permissions to not be the same as all directory permissions.
> 
find . -type d -exec chmod -v 0644 '{}' \;

to change the folder

find . -type f -exec chmod -v 0755 '{}' \;

to change files

Here's how to discriminate

-- 
Polyna-Maude R.-Summerside
-Be smart, Be wise, Support opensource development

[toc] | [prev] | [next] | [standalone]


#238232

FromChristian Groessler <chris@groessler.org>
Date2021-08-06 02:30 +0200
Message-ID<CIVUl-1lo-1@gated-at.bofh.it>
In reply to#238230
On 8/6/21 2:01 AM, Polyna-Maude Racicot-Summerside wrote:
> find . -type d -exec chmod -v 0644 '{}' \;
> 
> to change the folder
> 
> find . -type f -exec chmod -v 0755 '{}' \;
> 
> to change files


Pah. Use 'xargs' :-)

$ find . -type f -print0 | xargs -0 chmod 644
$ find . -type d -print0 | xargs -0 chmod 755


regards,
chris

[toc] | [prev] | [next] | [standalone]


#238234

FromChristian Groessler <chris@groessler.org>
Date2021-08-06 03:00 +0200
Message-ID<CIWnn-1ul-3@gated-at.bofh.it>
In reply to#238230
On 8/6/21 2:52 AM, David wrote:
> I was commenting on how I have always been puzzled why
> someone made the effort to give 'chmod' an '-R' option, but
> never made it actually useful for common cases. As it is,
> it seems that it's really only useful for modifying the write attribute.

Hmm. "chmod -R go-rwx ." also seems to be useful...
Just an example.

regards,
chris

[toc] | [prev] | [next] | [standalone]


#238236

FromGreg Wooledge <greg@wooledge.org>
Date2021-08-06 03:10 +0200
Message-ID<CIWx3-1MS-1@gated-at.bofh.it>
In reply to#238234
On Fri, Aug 06, 2021 at 02:58:00AM +0200, Christian Groessler wrote:
> On 8/6/21 2:52 AM, David wrote:
> > I was commenting on how I have always been puzzled why
> > someone made the effort to give 'chmod' an '-R' option, but
> > never made it actually useful for common cases. As it is,
> > it seems that it's really only useful for modifying the write attribute.
> 
> Hmm. "chmod -R go-rwx ." also seems to be useful...
> Just an example.

Yeah.  The -R option is basically only useful when using relative
permission changes, not absolute.  It's not an option that you can
use very often, but it's not *completely* useless.

[toc] | [prev] | [next] | [standalone]


#238237

FromDavid <bouncingcats@gmail.com>
Date2021-08-06 03:10 +0200
Message-ID<CIWx3-1MS-5@gated-at.bofh.it>
In reply to#238234
On Fri, 6 Aug 2021 at 10:58, Christian Groessler <chris@groessler.org> wrote:
> On 8/6/21 2:52 AM, David wrote:

> > I was commenting on how I have always been puzzled why
> > someone made the effort to give 'chmod' an '-R' option, but
> > never made it actually useful for common cases. As it is,
> > it seems that it's really only useful for modifying the write attribute.
>
> Hmm. "chmod -R go-rwx ." also seems to be useful...
> Just an example.

Thanks for the replies, which give me clues to things I might
have not considered.

I'm the only human user on my machines, so
managing other users isn't something I think about much.

[toc] | [prev] | [next] | [standalone]


#238235

FromDavid <bouncingcats@gmail.com>
Date2021-08-06 03:00 +0200
Message-ID<CIWnn-1ul-5@gated-at.bofh.it>
In reply to#238230
On Fri, 6 Aug 2021 at 10:01, Polyna-Maude Racicot-Summerside
<debian@polynamaude.com> wrote:
> On 2021-08-05 7:30 p.m., David wrote:
> > On Fri, 6 Aug 2021 at 06:03, Gary L. Roach <garyroach719@gmail.com> wrote:

> > 'chmod -R' is less useful because it does not discriminate
> > between files and directories, I never understood why it
> > does not offer that option, because usually we need all file
> > permissions to not be the same as all directory permissions.
> >
> find . -type d -exec chmod -v 0644 '{}' \;
>
> to change the folder
>
> find . -type f -exec chmod -v 0755 '{}' \;
>
> to change files
>
> Here's how to discriminate

Perhaps I wasn't clear.
I wasn't asking for an alternative method.

I was commenting on how I have always been puzzled why
someone made the effort to give 'chmod' an '-R' option, but
never made it actually useful for common cases. As it is,
it seems that it's really only useful for modifying the write attribute.

[toc] | [prev] | [next] | [standalone]


#238246

From<tomas@tuxteam.de>
Date2021-08-06 09:00 +0200
Message-ID<CJ1ZM-4XP-5@gated-at.bofh.it>
In reply to#238235

[Multipart message — attachments visible in raw view] — view raw

On Fri, Aug 06, 2021 at 10:52:51AM +1000, David wrote:

[...]

> I was commenting on how I have always been puzzled why
> someone made the effort to give 'chmod' an '-R' option, but
> never made it actually useful for common cases. As it is,
> it seems that it's really only useful for modifying the write attribute.

It doesn't cover all the cases, but you're aware of the 'X' variant
for chmod (aka: turn on the x bit if a directory, but also if it
has already an x bit somewhere else, alas):

  tomas@trotzki:~$ ls -ald f*b
  -rw-r--r-- 1 tomas tomas    0 Aug  6 08:49 fib
  drwxr-xr-x 2 tomas tomas 4096 Aug  6 08:48 fob
  -rwxr--r-- 1 tomas tomas    0 Aug  6 08:48 fub

  tomas@trotzki:~$ chmod go+X f?b

  tomas@trotzki:~$ ls -ald f*b
  -rw-r--r-- 1 tomas tomas    0 Aug  6 08:49 fib
  drwxr-xr-x 2 tomas tomas 4096 Aug  6 08:48 fob
  -rwxr-xr-x 1 tomas tomas    0 Aug  6 08:48 fub

It might /sometimes/ do what you want (i.e. in the most common case
where you want dirs to have the x bit set, but regular files not).

Cheers
 - t

[toc] | [prev] | [next] | [standalone]


#238240

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2021-08-06 05:10 +0200
Message-ID<CIYpc-2Ww-9@gated-at.bofh.it>
In reply to#238228
On Fri 06 Aug 2021 at 09:30:17 (+1000), David wrote:
> On Fri, 6 Aug 2021 at 06:03, Gary L. Roach <garyroach719@gmail.com> wrote:
> 
> > Second, why am I separating out the Path the way I am doing? I need to
> > check each level for existence then, if the level doesn't exist, create
> > the directory, cd to the directory, set chown and -x chmod. After that
> > check the next level and repeat the process until I run out of levels.
> > There are other things that need to be done with files that are similar.
> 
> It sounds like you are unaware of 'mkdir -p' and 'chown -R'.
> They are the standard tools for this kind of task.
> 
> 'chmod -R' is less useful because it does not discriminate
> between files and directories, I never understood why it
> does not offer that option, because usually we need all file
> permissions to not be the same as all directory permissions.

?

I thought that was what X is for. A demonstration:

1. Create a tree of directories and files, making them
   available only to the user:

$ mkdir -p temp/tree/{a,b} temp/tree/{a/{p,q,r},b/{x,y,z}}
$ touch temp/tree/{a,b}/text temp/tree/{a/{p,q,r},b/{x,y,z}}/text
$ chmod -R go= temp/tree
$ ls -GlgR temp/tree | grep -v total
temp/tree:
drwx------ 5 4096 Aug  5 21:55 a
drwx------ 5 4096 Aug  5 21:55 b

temp/tree/a:
drwx------ 2 4096 Aug  5 21:55 p
drwx------ 2 4096 Aug  5 21:55 q
drwx------ 2 4096 Aug  5 21:55 r
-rw------- 1    0 Aug  5 21:55 text

temp/tree/a/p:
-rw------- 1 0 Aug  5 21:55 text

temp/tree/a/q:
-rw------- 1 0 Aug  5 21:55 text

temp/tree/a/r:
-rw------- 1 0 Aug  5 21:55 text

temp/tree/b:
-rw------- 1    0 Aug  5 21:55 text
drwx------ 2 4096 Aug  5 21:55 x
drwx------ 2 4096 Aug  5 21:55 y
drwx------ 2 4096 Aug  5 21:55 z

temp/tree/b/x:
-rw------- 1 0 Aug  5 21:55 text

temp/tree/b/y:
-rw------- 1 0 Aug  5 21:55 text

temp/tree/b/z:
-rw------- 1 0 Aug  5 21:55 text
$ 

2. Now use X to make them readable for all:

$ chmod -R a+rX temp/tree
$ ls -GlgR temp/tree | grep -v total
temp/tree:
drwxr-xr-x 5 4096 Aug  5 21:55 a
drwxr-xr-x 5 4096 Aug  5 21:55 b

temp/tree/a:
drwxr-xr-x 2 4096 Aug  5 21:55 p
drwxr-xr-x 2 4096 Aug  5 21:55 q
drwxr-xr-x 2 4096 Aug  5 21:55 r
-rw-r--r-- 1    0 Aug  5 21:55 text

temp/tree/a/p:
-rw-r--r-- 1 0 Aug  5 21:55 text

temp/tree/a/q:
-rw-r--r-- 1 0 Aug  5 21:55 text

temp/tree/a/r:
-rw-r--r-- 1 0 Aug  5 21:55 text

temp/tree/b:
-rw-r--r-- 1    0 Aug  5 21:55 text
drwxr-xr-x 2 4096 Aug  5 21:55 x
drwxr-xr-x 2 4096 Aug  5 21:55 y
drwxr-xr-x 2 4096 Aug  5 21:55 z

temp/tree/b/x:
-rw-r--r-- 1 0 Aug  5 21:55 text

temp/tree/b/y:
-rw-r--r-- 1 0 Aug  5 21:55 text

temp/tree/b/z:
-rw-r--r-- 1 0 Aug  5 21:55 text
$ 

All the directories get execute permission.

Cheers,
David.

[toc] | [prev] | [next] | [standalone]


#238241

FromDavid <bouncingcats@gmail.com>
Date2021-08-06 05:50 +0200
Message-ID<CIZ1U-38z-3@gated-at.bofh.it>
In reply to#238240
On Fri, 6 Aug 2021 at 13:06, David Wright <deblis@lionunicorn.co.uk> wrote:
> On Fri 06 Aug 2021 at 09:30:17 (+1000), David wrote:

> > 'chmod -R' is less useful because it does not discriminate
> > between files and directories, I never understood why it
> > does not offer that option, because usually we need all file
> > permissions to not be the same as all directory permissions.
>
> ?
>
> I thought that was what X is for. A demonstration:

[...]

> All the directories get execute permission.

Thank you very much for alerting me to X.
I had never noticed it before.
This is why I asked, to get feedback in case
I was overlooking something.

A few years ago I wrote a shell script to do recursive
chmod that allows different permissions to be specified
for subdirectories and files under a specified top directory,
so I never thought about this issue again after that.

[toc] | [prev] | [next] | [standalone]


#238248

FromDarac Marjal <mailinglist@darac.org.uk>
Date2021-08-06 09:20 +0200
Message-ID<CJ2j7-5kG-5@gated-at.bofh.it>
In reply to#238228

[Multipart message — attachments visible in raw view] — view raw

On 06/08/2021 00:30, David wrote:
> On Fri, 6 Aug 2021 at 06:03, Gary L. Roach <garyroach719@gmail.com> wrote:
>
>> Second, why am I separating out the Path the way I am doing? I need to
>> check each level for existence then, if the level doesn't exist, create
>> the directory, cd to the directory, set chown and -x chmod. After that
>> check the next level and repeat the process until I run out of levels.
>> There are other things that need to be done with files that are similar.
> It sounds like you are unaware of 'mkdir -p' and 'chown -R'.
> They are the standard tools for this kind of task.
>
> 'chmod -R' is less useful because it does not discriminate
> between files and directories, I never understood why it
> does not offer that option, because usually we need all file
> permissions to not be the same as all directory permissions.

I also recently discovered that 'mkdir' has a '-m, --mode' option so
that, depending on your use case, you might not even need chmod. 'mkdir
-m u=rwx,g=rx,o= -p /path/to/somewhere' for example.

>

[toc] | [prev] | [next] | [standalone]


#238227

FromTom Browder <tom.browder@gmail.com>
Date2021-08-06 01:00 +0200
Message-ID<CIUvf-pk-1@gated-at.bofh.it>
In reply to#238168

[Multipart message — attachments visible in raw view] — view raw

On Wed, Aug 4, 2021 at 18:58 Gary L. Roach <garyroach719@gmail.com> wrote:

> Hi all;
> I have just recently delved into the magical world of Bash scripting and


I long ago gave up bash scripting for other than simple scripts, even for
sysadmin chores.

Most Linux distros, including our favorite Debian, come with Perl installed
in the standard distro, and it’s a quick upgrade to add Raku as well (
https://Raku.org).

Both languages are great “glue” languages and are C-like as opposed to
Python. Raku, in particular, has a much easier syntax than bash. And the
payoff is learning a much more powerful language with a huge
problem-solving domain

Best regards,

-Tom

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | linux.debian.user


csiph-web