NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Scripting for /bin/ksh



Thanks! This is a first for me. I rarely do anything with /bin/ksh. I use PERL.
But if I need to reinstall the system, I wont have PERL. I need to get around
this.

On Wed, Aug 4, 2021 at 6:01 PM Chavdar Ivanov <ci4ic4%gmail.com@localhost> wrote:
>
> On Wed, 4 Aug 2021 at 22:30, Todd Gruhn <tgruhn2%gmail.com@localhost> wrote:
> >
> > Thanks for the link, Lewis. I have never seen this site. I DID find a
> > book in PDF format
> > regarding /bin/ksh programming.
> >
> > My new problem is testing user rank/id.
> >
> > I want to see if the user is "root"
> >
> > if (user != root), then print message and die.
>
> #  [[ $(id -u)  -eq  0 ]] && echo  "you are root"
> you are root
>
>
>
> >
> > This is easy in PERL; I have not done it in /bin/ksh.
> > I would also like it to execute this test before running the borg command and
> > spitting out junk...
> >
> > On Wed, Aug 4, 2021 at 2:47 PM J. Lewis Muir <jlmuir%imca-cat.org@localhost> wrote:
> > >
> > > On 08/02, Chavdar Ivanov wrote:
> > > > On Mon, 2 Aug 2021 at 14:16, Todd Gruhn <tgruhn2%gmail.com@localhost> wrote:
> > > > >
> > > > > Thanks for the code Matt.
> > > > > I will try this.
> > > > > By 'execute' I mean generate
> > > > > ${cmd}
> > > > > then execute/do whatever ${cmd} turns out to be.
> > > >
> > > > Depending on the contents of cmd, you might have to use
> > > >
> > > > eval ${cmd}
> > >
> > > Yes, and there's the rub: the corner cases.  To correctly build up
> > > a command like this and execute it where spaces and other special
> > > characters are parsed correctly, you have to shell-quote cmd before
> > > passing it to eval.  See the "Shell-quoting arbitrary strings" section
> > > in:
> > >
> > >   https://www.etalabs.net/sh_tricks.html
> > >
> > > For example, here's a test program that correctly constructs and
> > > executes two commands, touch and ls, to create and list some
> > > "interesting" test files:
> > >
> > > ----
> > > #!/bin/ksh
> > >
> > > set -e
> > >
> > > # https://www.etalabs.net/sh_tricks.html
> > > quote() {
> > >   printf %s\\n "$1" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"
> > > }
> > >
> > > # https://dwheeler.com/essays/filenames-in-shell.html
> > > nl=$(printf '\nX')
> > > nl=${nl%X}
> > >
> > > f1='test_a space'
> > > f2='test_b"double-quote'
> > > f3="test_c'single-quote"
> > > f4="test_d${nl}newline"
> > > f5='test_e\backslash'
> > >
> > > f1_quoted=$(quote "$f1")
> > > f2_quoted=$(quote "$f2")
> > > f3_quoted=$(quote "$f3")
> > > f4_quoted=$(quote "$f4")
> > > f5_quoted=$(quote "$f5")
> > >
> > > cmd="touch $f1_quoted $f2_quoted $f3_quoted $f4_quoted $f5_quoted"
> > > printf 'cmd: %s\n' "$cmd"
> > > eval "$cmd"
> > > cmd="ls -B1 $f1_quoted $f2_quoted $f3_quoted $f4_quoted $f5_quoted"
> > > printf 'cmd: %s\n' "$cmd"
> > > eval "$cmd"
> > > ----
> > >
> > > Lewis
>
>
>
> --
> ----


Home | Main Index | Thread Index | Old Index