Tiny Core Linux

General TC => Programming & Scripting - Unofficial => Topic started by: aus9 on July 15, 2023, 09:40:00 AM

Title: [Solved] variable expansion inside a here-file issue
Post by: aus9 on July 15, 2023, 09:40:00 AM
Hi

most tiimes, a script that cats a line of text into a /pathway2/file works for me.

I am close on this one
all the lines in a terminal look like
> line1
> line 2 etc
my script is also pasting a tce.install script so I use EOF and that looks good as
> EOF

Up the top I change the demiliter to ZZZ and the rest of the lines look good until the last line
which reads as

> ZZZ
but that is supposed to be end of the file.

I have used ctrl + c to close down attempted input

Any clues ?
here is my top middle and bottom lines
( I have checked with echo that the variables are good)

cat > $P/usr/local/share/$P/files/$GET <<ZZZ

# tce.install
EOF

ZZZ

thanks for reading
Title: Re: script has 2 EOF and not working
Post by: Paul_123 on July 15, 2023, 10:36:09 AM
It’s hard to follow script posts with very fragmented lines of code.  Post the full script or a bigger portion of the code.
Title: Re: script has 2 EOF and not working
Post by: patrikg on July 15, 2023, 01:21:56 PM
Have you tried to add it with double quotes like this example.

Code: (bash) [Select]
cat > /etc/resolv.conf << "EOF"
nameserver 8.8.8.8
nameserver 8.8.4.4
EOF
Title: Re: script has 2 EOF and not working
Post by: jazzbiker on July 15, 2023, 02:23:16 PM
I don't know whether I've understood the problem correctly but for me:
Code: [Select]
tc@box:~$ cat > bbb <<ZZZ
> cat > ccc <<EOF
> ls
> EOF
> ZZZ
tc@box:~$ cat bbb
cat > ccc <<EOF
ls
EOF
tc@box:~$ . ./bbb
tc@box:~$ cat ccc
ls

looks like nested here-files work.
Title: Re: script has 2 EOF and not working
Post by: aus9 on July 15, 2023, 09:40:14 PM
Hi Rich
Please mark as solved.

The solution was a test based on patrikg reply but with a small change away from ZZZ
Code: [Select]
cd /tmp && sudo su

cat > test << "EOF2"
#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi

P=get-chromium
TCDIR=/etc/sysconfig/tcedir/optional

# tce.install
###########
mkdir -p $P/usr/local/tce.installed
cat > $P/usr/local/tce.installed/$P << 'EOF'
#!/bin/sh
[  -f /var/run/dbus/pid ] || /usr/local/etc/init.d/dbus start
EOF
chown -R root:staff  $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# now we put into member's tcedir
cd $TCDIR
cp /tmp/$P.tcz*  .

EOF2
Note that I am reluctant to post full code, as I am simple coder, I need to build the get TCE and then test it. I have a fair chance I need to improve it and do not want for others to point out all my mistakes....before I get a chance to see some for myself.  ;D

Note this is a private TCE built for private purposes so I am allowed to autostart dbus for this target member group. The size of the download and the resultant TCE may scare some members on arm so I suspect very few will want it.

Thanks for reading
Title: Re: [Solved] script has 2 EOF and not working
Post by: Rich on July 15, 2023, 11:02:24 PM
Hi aus9
Please mark as solved. ...
Done.  :)
Title: Re: [Solved] script has 2 EOF and not working
Post by: aus9 on July 15, 2023, 11:04:50 PM
Hi Rich sorry full script fails I am going to have to post it.
can you remove solved and in next post I will have to show full output

the end shows
> EOF2
I press enter expecting to return to prompt but what I see is
> EOF2
>
Title: Re: [Solved] script has 2 EOF and not working
Post by: aus9 on July 15, 2023, 11:21:14 PM
server error in attempting to post...so a clickable txt file link to show what I was attempting.

https://www.dropbox.com/scl/fi/bbkk2xfmh4uu555lmp2ye/forum.txt?rlkey=x7utnunbipvdkyrad68w2m8y5&dl=0

ok just spotted that my first echo has multiple double quotes..I will remove and retest
EDIT2 Nope still fails

my brain needs a rest. thanks for reading
Title: Re: script has 2 EOF and not working
Post by: Rich on July 16, 2023, 12:39:26 AM
Hi aus9
... can you remove solved ...
Removed.  :o
Title: Re: script has 2 EOF and not working
Post by: jazzbiker on July 16, 2023, 03:57:10 AM
Hi aus9,

In Your script You are assigning variables inside the here-file. This is funny and tricky but may cause brain damage )
Code: [Select]
tc@box:~$ cat aaa
cat > bbb << EOF2
N=abc
cat > $N << EOF
ls
EOF
EOF2
tc@box:~$ . ./aaa
tc@box:~$ cat bbb
N=abc
cat >  << EOF
ls
EOF

Can You rewrite Your script at least to move variables definitions out of the here-file fragment?

Welcome to hell scripting!
Title: Re: script has 2 EOF and not working
Post by: Paul_123 on July 16, 2023, 07:40:18 AM
Variable expansion happens as the script is written too.  Can be really tricky

Why do you need to write the script from a script?  Can you just include the script as a file?
Title: Re: script has 2 EOF and not working
Post by: aus9 on July 16, 2023, 09:16:47 AM
Thanks Paul_123
I am glad others are seeing some issue too.

Lets drop this. I can upload it as a file -> in my build script use wget to get it and include it the submission under src/

Hi jazzbiker
I try to have as much text stuff inside my build script. But I give up on this one.
The build script includes a help file, well more of a howto as I will be leaving it to the victim oops I mean the member to maintain their get-script  ;D
and thanks for your feedback

Hi Rich
Please mark as solved and at your discretion change subject to something like
variable expansion inside a here-file issue or anything you think might better describe what happened.


Title: Re: script has 2 EOF and not working
Post by: patrikg on July 16, 2023, 09:21:46 AM
You could also test to use some another variable substitution, with the curly brackets.
Like this. or do you need the dollar sign variable in the script you make, you need to escape that character because of the variable substitution.

Code: (bash) [Select]
cat > ${N} << EOF

Code: (bash) [Select]
cat > \${N} << EOF

or like your way.

Code: (bash) [Select]
cat > \$N << EOF
Title: Re: [Solved] variable expansion inside a here-file issue
Post by: Rich on July 16, 2023, 09:42:45 AM
Hi aus9
... Please mark as solved and at your discretion change subject to something like
variable expansion inside a here-file issue ...
Done.
Title: Re: script has 2 EOF and not working
Post by: jazzbiker on July 16, 2023, 10:00:42 AM
You could also test to use some another variable substitution, with the curly brackets.
Like this. or do you need the dollar sign variable in the script you make, you need to escape that character because of the variable substitution.

Code: (bash) [Select]
cat > ${N} << EOF

Code: (bash) [Select]
cat > \${N} << EOF

or like your way.

Code: (bash) [Select]
cat > \$N << EOF

Hi patrikg,

Sorry but not
Code: [Select]
tc@box:~$ cat aaa
cat > bbb << EOF2
N=abc
cat > ${N} << EOF
ls
EOF
EOF2
tc@box:~$ . ./aaa
tc@box:~$ cat bbb
N=abc
cat >  << EOF
ls
EOF

The issue aus9 faced is related to the variables expansion as Paul_123 noted. This is not syntax error. Don't sit to play dice with the devil. Simply don't do it )

Have a nice Core!
Title: Re: [Solved] variable expansion inside a here-file issue
Post by: patrikg on July 17, 2023, 05:00:37 AM
I can't get this of my head, just remembers that linux use two variables settings.
First you have the shell variables, which is not transferred between parent to child.
And you have something called environment variables. that stays between parent to child.

So if you want the variable to stay between parent to child you need to export it.

If i don't misunderstand your problem it seems to maybe be that the shell spawn's another shell and don't inherit the variable between parent to child.

So maybe when you assign your variable within your shell, just add the command export in front of it like this:
Code: (bash) [Select]
export N="Test"

Code: (bash) [Select]
not like this:
N="Test"

Lets play with this like this.
Code: (bash) [Select]
> TEST="Test"
> echo $TEST
Test
> bash
> echo $TEST

> exit
exit
> echo $TEST
Test
> export TEST
> bash
> echo $TEST
Test
> exit
exit
> export TEST1="Test2"
> bash
> echo $TEST1
Test2
> exit
exit

Title: Re: [Solved] variable expansion inside a here-file issue
Post by: jazzbiker on July 17, 2023, 07:52:33 AM
Hi patrikg,

You are quite right about exported variables. But if we are discussing the issue that broke aus9's script it crawls from another grim door named "here-files" or maybe "here-documents". Are You acquainted with this topic?
Title: Re: [Solved] variable expansion inside a here-file issue
Post by: GNUser on July 17, 2023, 10:21:37 AM
the issue that broke aus9's script...crawls from another grim door...
Hi, Andrey. Thanks for making me laugh ;D

What I like best about GNU/Linux is that there are no "Keep Out" signs anywhere. Other OSes seem not to have grim doors and crawling things--but that's only because the user is not allowed to look around.
Title: Re: [Solved] variable expansion inside a here-file issue
Post by: patrikg on July 17, 2023, 10:36:00 AM
Hi patrikg,

You are quite right about exported variables. But if we are discussing the issue that broke aus9's script it crawls from another grim door named "here-files" or maybe "here-documents". Are You acquainted with this topic?

Yes I am familer with this type of word and there meaning.
Okey I get it, I think.
Like the good movie Inception (https://www.imdb.com/title/tt1375666/), nested/recursion things like dream in a dream, and what that is being supported with heredoc or herefile.
Title: Re: [Solved] variable expansion inside a here-file issue
Post by: GNUser on July 17, 2023, 11:07:37 AM
I would try 1000 other approaches before considering a nested here document. I don't like pain.
Title: Re: [Solved] variable expansion inside a here-file issue
Post by: jazzbiker on July 17, 2023, 11:07:50 AM
Hi, Andrey. Thanks for making me laugh ;D

What I like best about GNU/Linux is that there are no "Keep Out" signs anywhere. Other OSes seem not to have grim doors and crawling things--but that's only because the user is not allowed to look around.

Hi Bruno,

I like You validate my jokes about shell ) But, seriously, I'm scared with it ( It doesn't play fairly. Still probably this is my personal attitude, I wrote a lot in various assemblers so You can believe me I am quite comfortable without any doors and even walls at all ) Nice that You keep calm and happy using shell! Hope we will appeal to You as the last resort when the next time shell will put the gun to the head of the next innocent programmer threatening to push the trigger. Damn, I am frightening myself. Time to stop )
Title: Re: [Solved] variable expansion inside a here-file issue
Post by: jazzbiker on July 17, 2023, 11:09:04 AM
I would try 1000 other approaches before considering a nested here document. I don't like pain.

Well, this is said by man who enjoy shell! Worth believing.
Title: Re: [Solved] variable expansion inside a here-file issue
Post by: jazzbiker on July 17, 2023, 11:16:02 AM
Yes I am familer with this type of word and there meaning.
Okey I get it, I think.

The trouble occurs because inside the here-file fragment assignments are not performed, while variables expansion takes place. Inside the here-file the text is simply text, not the source. Try to examine aus9's script from this point of view.