WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: [Solved] variable expansion inside a here-file issue  (Read 4019 times)

aus9

  • Guest
[Solved] variable expansion inside a here-file issue
« 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
« Last Edit: July 16, 2023, 09:38:40 AM by Rich »

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1237
Re: script has 2 EOF and not working
« Reply #1 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.

Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 705
Re: script has 2 EOF and not working
« Reply #2 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
« Last Edit: July 15, 2023, 01:23:44 PM by patrikg »

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 934
Re: script has 2 EOF and not working
« Reply #3 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.

aus9

  • Guest
Re: script has 2 EOF and not working
« Reply #4 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
« Last Edit: July 15, 2023, 09:45:18 PM by aus9 »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11560
Re: [Solved] script has 2 EOF and not working
« Reply #5 on: July 15, 2023, 11:02:24 PM »
Hi aus9
Please mark as solved. ...
Done.  :)

aus9

  • Guest
Re: [Solved] script has 2 EOF and not working
« Reply #6 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
>

aus9

  • Guest
Re: [Solved] script has 2 EOF and not working
« Reply #7 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
« Last Edit: July 15, 2023, 11:34:15 PM by aus9 »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11560
Re: script has 2 EOF and not working
« Reply #8 on: July 16, 2023, 12:39:26 AM »
Hi aus9
... can you remove solved ...
Removed.  :o

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 934
Re: script has 2 EOF and not working
« Reply #9 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!
« Last Edit: July 16, 2023, 04:01:26 AM by jazzbiker »

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1237
Re: script has 2 EOF and not working
« Reply #10 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?

aus9

  • Guest
Re: script has 2 EOF and not working
« Reply #11 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.



Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 705
Re: script has 2 EOF and not working
« Reply #12 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

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11560
Re: [Solved] variable expansion inside a here-file issue
« Reply #13 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.

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 934
Re: script has 2 EOF and not working
« Reply #14 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!