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


Groups > alt.os.linux > #59926

Re: Bash topics (Memory Usage)

From Dan Purgert <dan@djph.net>
Newsgroups alt.os.linux
Subject Re: Bash topics (Memory Usage)
Date 2019-10-20 13:54 +0000
Organization A noiseless patient Spider
Message-ID <slrnqqoppi.1ug.dan@djph.net> (permalink)
References <qoej8c$9r5$1@dont-email.me> <qofv1n$p8o$1@dont-email.me> <qohms9$83v$1@dont-email.me>

Show all headers | View raw


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Soviet_Mario wrote:
> On 19/10/2019 23:26, William Unruh wrote:
>> It is a "progamming" language, and a scripting language. It is however
>> slow and tends to rely on other programs to accomplish its tasks.
>> 
>> But why do you care what it is called? 
>
> no, I'm "caring" about its nature to try to understand WHY 
> with this language and not others I cannot get anything done.
> I mean, I self-taught basic, C, to a lesser extent C++, 
> Pascal (and long ago somewhat Fortran 77, but I HATED IT ... 
> never the less I understood it), but I absolutely cannot 
> oriend in BASH. I just don't understand the logic, the 
> philosopy underlying.

At some level, a bash script is simply some "glue logic" to operating on
the system; so you don't have to write all the commands out for
"everything".

For example, to rename all "*tar.gz" files to "*tgz":

  for f in *tar.gz; do g=${f%%.tar.gz}; mv $f $g.tgz; done

Sticking that in a script ("rename") will do this:


  $> ls
     a.tar.gz b.tar.gz c.tar.gz d.tar.gz rename
  $> bash rename
  $> ls
     a.tgz b.tgz c.tgz d.tgz rename

NOTE -> the "bash rename" command is done this way because in our
example, I haven't gone through the trouble to make the "rename" script
executable.  You can make the script executable with the command "chmod
+x rename" and then just run it as "./rename" instead.

So, with "one command" (well, script), we renamed the four files,
instead of having to type it out every time:

  $> ls 
     a.tar.gz b.tar.gz c.tar.gz d.tar.gz
  $> mv a.tar.gz a.tgz
  $> mv b.tar.gz b.tar
  $> mv c.tar.gz c.tgz
  $> mv d.tar.gz d.tar
  $> ls
     a.tgz b.tgz c.tgz d.tgz

That's it.  Sure, there is some additional logic you can add in
(conditional statements, some basic math, etc.) to help control the
program flow, but in my experience, scripts tend to be "one liners" like
the above.

> [...]
> So I'm suspecting sth in my mind-shape is not compatible 
> with scripting shell, and I was trying to understand what.
> It's a vague question not easy to be expressed though

TBH, it sounds more like you're just trying to make it harder than it
really is.


>
> in C++ the same occurred to me using a lot of MACRO 
> preprocessor : I failed to figure out the GENERATED CODE. If 
> sth is non directly visible I fail to prefigure the effects.

going from C(++) and then trying to understand the assembly output is a
considerably more complex task than understanding bash scripting.

>
> with BASH i cannot even figure out the level of 
> "indirections" (= how many time a certain expression will be 
> expanded and why). And assumed I understood theoretically, 
> it would still be beyond my prefiguration ability.

So in the above script, there are two expansions we're working with:

  1. The glob of "*.tar.gz" -- it only finds filenames ending with
      "*.tar.gz".  Other filenames (in the example "rename") are
      ignored.

  2. The parameter expansion of "${f%%.tar.gz}" -- it means "expand the 
      word contained in the variable, then delete the longest string of 
      characters matching .tar.gz from the end of the word".  So in our
      example, "a.tar.gz" would end up being "a" after going through
      that parameter expansion.

NOTE -> in the script, we're assigning the variable "g" to the output of
the pattern, with the string ".tgz" tacked on the end, right before
issuing the command.

The actual execution of the script can be psuedo-coded as follows:

for f in *tar.gz <-- "for a.tar.gz, b.tar.gz, c.tar.gz, and d. tar.gz
do g=${f%%.tar.gz} <-- set g to the filename, less ".tar.gz"
mv $f ${g}.tgz <-- move the file named in variable "f" to the shortened
name held in "g", with ".tgz" tacked on the end

so, it does this
(first loop)
f=a.tar.gz
g=a
mv a.tar.gz a.tgz
(second loop)
f=b.tar.gz
g=b
mv b.tar.gz b.tgz
(third loop)
f=c.tar.gz
g=c
mv c.tar.gz c.tgz
(fourth loop)
f=d.tar.gz
g=d
mv d.tar.gz d.tgz

HTH

-----BEGIN PGP SIGNATURE-----

iQEzBAEBCAAdFiEEBcqaUD8uEzVNxUrujhHd8xJ5ooEFAl2sZzAACgkQjhHd8xJ5
ooFotAf/eoO3BOqJa9Mp5bODde60G4pe5dN3JGhM4w11bEM7WxUri2KA565IVCRB
Ew2zKhpkF6pqmv7rxyT86v4gGbv3owsXUJR73nOPZnnM3+Hk+rzVUWCieSswdpGD
r3iEQH5sAgMD8H9oWBBydvSNSD4HNMrAKWjD+KnAXKVhQsx0x1DFUNiPvwh0XQNb
bjVt24lAuz8tArmQj+GeM9O/+NxuF4NZonJOMhcN84cAHdYWGalJ1gtiMi7mV1ep
6M34j3YBMBRnbrjQhM8VFbWzsyiIzAgjsRaJxVRSU4/zN3puoWqMngKUX4yG+4Hg
9fjFW9Ds3zkM0BiYKcGozHDwt2YKwA==
=XTLt
-----END PGP SIGNATURE-----

-- 
|_|O|_| 
|_|_|O| Github: https://github.com/dpurgert
|O|O|O| PGP: 05CA 9A50 3F2E 1335 4DC5  4AEE 8E11 DDF3 1279 A281

Back to alt.os.linux | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Bash topics (Memory Usage) Soviet_Mario <SovietMario@CCCP.MIR> - 2019-10-19 10:58 +0200
  Re: Bash topics (Memory Usage) Dan Purgert <dan@djph.net> - 2019-10-19 13:15 +0000
    Re: Bash topics (Memory Usage) Aragorn <thorongil@telenet.be> - 2019-10-19 15:44 +0200
      Re: Bash topics (Memory Usage) Soviet_Mario <SovietMario@CCCP.MIR> - 2019-10-20 01:15 +0200
        Re: Bash topics (Memory Usage) Dan Purgert <dan@djph.net> - 2019-10-19 23:25 +0000
        Re: Bash topics (Memory Usage) Aragorn <thorongil@telenet.be> - 2019-10-20 12:33 +0200
          Re: Bash topics (Memory Usage) "Carlos E.R." <robin_listas@es.invalid> - 2019-10-20 13:39 +0200
            Re: Bash topics (Memory Usage) Aragorn <thorongil@telenet.be> - 2019-10-20 16:19 +0200
              Re: Bash topics (Memory Usage) "Carlos E.R." <robin_listas@es.invalid> - 2019-10-20 21:08 +0200
                Re: Bash topics (Memory Usage) Aragorn <thorongil@telenet.be> - 2019-10-20 21:51 +0200
                Re: Bash topics (Memory Usage) "Carlos E.R." <robin_listas@es.invalid> - 2019-10-20 22:12 +0200
    Re: Bash topics (Memory Usage) John Hasler <jhasler@newsguy.com> - 2019-10-19 12:33 -0500
  Re: Bash topics (Memory Usage) William Unruh <unruh@invalid.ca> - 2019-10-19 21:26 +0000
    Linux users who buttfuck the president... Idaho Homo Joe <dick_lick@aol.com> - 2019-10-19 21:47 -0400
    Re: Bash topics (Memory Usage) Soviet_Mario <SovietMario@CCCP.MIR> - 2019-10-20 15:19 +0200
      Re: Bash topics (Memory Usage) Dan Purgert <dan@djph.net> - 2019-10-20 13:54 +0000
      Re: Bash topics (Memory Usage) Richard Kettlewell <invalid@invalid.invalid> - 2019-10-20 15:29 +0100
        Re: Bash topics (Memory Usage) William Unruh <unruh@invalid.ca> - 2019-10-20 19:24 +0000
          Re: Bash topics (Memory Usage) Richard Kettlewell <invalid@invalid.invalid> - 2019-10-21 10:28 +0100
      Re: Bash topics (Memory Usage) Chris <ithinkiam@gmail.com> - 2019-10-20 15:36 +0100
      Re: Bash topics (Memory Usage) Paul <nospam@needed.invalid> - 2019-10-20 11:05 -0400
        Re: Bash topics (Memory Usage) Soviet_Mario <SovietMario@CCCP.MIR> - 2019-10-20 23:14 +0200
          Re: Bash topics (Memory Usage) John Hasler <jhasler@newsguy.com> - 2019-10-20 18:01 -0500
            Re: Bash topics (Memory Usage) Paul <nospam@needed.invalid> - 2019-10-20 22:03 -0400

csiph-web