LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-02-2020, 10:12 AM   #1
nkamp
LQ Newbie
 
Registered: Aug 2008
Posts: 25

Rep: Reputation: 0
Shell script, what does this line do: "cat -- << EOF >${CreateScript_DeployDirs}"


Hello,

I'm analyzing a script from someone else and I'm asking what does this line do?

In short I have made a test script, trying to understand how it works..., but it is not clear to me:
Code:
set -euo pipefail
LocalPath="/home" 
TempPath="nkamp/$(date +%Y-%m-%d)"

function DeployReleaseKit() {

        local CreateScript_DeployDirs=${LocalPath}/${TempPath}/linux/deploy
        local BackupScript_Previous="BackupScript_dir"

        #Deploy the releasekit
        echo "1. Start function start deploy Releasekit"

        cat -- << EOF >${CreateScript_DeployDirs}
#!/bin/bash        
echo -n
echo "2. indien getoond is de local variabel ${CreateScript_DeployDirs} een waarde"
exit 0
EOF

        cat -- << EOF >${BackupScript_Previous}

echo -n
echo " 3. indien dit getoond wordt heeft de variabele ${BackupScript_Previous} een waarde!" 
EOF

        echo "Dit is het eind van de functie - DeployReleaseKit -"

}

echo "Roep de functie DeployReleaseKit aan" #  var: CreateScript_DeployDirs: kan niet = local - BackupScript_Previous: ${BackupScript_Previous}" 
DeployReleaseKit
echo "De functie deployReleaseKit is aangeroepen" #  var: CreateScript_DeployDirs: kan niet = local - BackupScript_Previous: ${BackupScript_Previous} " 

exit 0
~
My expectations is that, if the variable {CreateScript_DeployDirs} has a value than the part till the first EOF is not executed. If it does not have a value than the part is till EOF is executed. Or just the other way around.

But this is the result:
Quote:
[nkamp@192 ~]$ ./test.sh
Roep de functie DeployReleaseKit aan
1. Start function start deploy Releasekit
./test.sh: line 14: /home/nkamp/2020-09-02/linux/deploy: Is a directory

[nkamp@192 ~]$ ./test.sh
Roep de functie DeployReleaseKit aan
1. Start function start deploy Releasekit
./test.sh: line 14: //linux/deploy: No such file or directory
This was not what I expected. (first result: the vars. ${LocalPath} and ${TempPath} does have a right value. Second result not)

I hope that someone can explain how this work and why and/or when such a construction should be used.

Nico.
 
Old 09-02-2020, 11:25 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,927

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
cat will write the text between EOFs into the named file. If that was possible (=writing that file).
 
Old 09-02-2020, 11:33 AM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,141
Blog Entries: 6

Rep: Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828
This does not do anything, there is nothing to cat.
Code:
cat -- << EOF > ${CreateScript_DeployDirs}
>
Couple of Examples:
Code:
a=1
cat << EOF
$a
$(($a + 1))
$(($a * 5))
EOF
1
2
5


b=2
cat << EOF > test.txt
c=$(($b * 3))
d=$(($c / 2))
EOF

cat test.txt
c=6
d=3
 
Old 09-02-2020, 11:46 AM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,141
Blog Entries: 6

Rep: Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828
That's a little bit of a mess. Is this what you are trying to do?
Code:
cat -- << EOF > test.txt       
echo -n
echo "2. indien getoond is de local variabel ${CreateScript_DeployDirs} een waarde"
exit 0
EOF

cat test.txt
echo -n
echo "2. indien getoond is de local variabel /home/nkamp/2020-09-02/linux/deploy een waarde"
exit 0
Edit:
Code:
LocalPath="/home" 
TempPath="nkamp/$(date +%Y-%m-%d)"

echo "$LocalPath"
/home

echo "$TempPath"
nkamp/2020-09-02

CreateScript_DeployDirs=${LocalPath}/${TempPath}/linux/deploy
echo "$CreateScript_DeployDirs"
/home/nkamp/2020-09-02/linux/deploy
Ok, where did you make that directory path so that you can write to it? So far all that you have is a variable.

Last edited by teckk; 09-02-2020 at 11:48 AM.
 
Old 09-03-2020, 02:19 AM   #5
nkamp
LQ Newbie
 
Registered: Aug 2008
Posts: 25

Original Poster
Rep: Reputation: 0
Hello,

Now I see it what this part is doing:
Code:
cat -- << EOF >${CreateScript_DeployDirs}
#!/bin/bash        
echo -n
echo "2. indien getoond is de local variabel ${CreateScript_DeployDirs} een waarde"
exit 0
EOF
This is my 'simple' example try to understand what this is doing. But I see now in the 'real' scripts which I'm analyzing do have in other variables the script (*.sh) names.

But I was not aware what that this was possible to write in a file with cat -- << EOF >${variable}...EOF. Very powerfull from my point of view.

Thanks.

Nico
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command line execution error: unexpected EOF while looking for matching `"' bash: -c: line 25: syntax error: unexpected end of file maheshreddy690 Linux - Newbie 1 12-25-2018 01:13 PM
[SOLVED] Insert value of variable using '( cat <<<'EOF' text here )> $newvar || What's wrong with me? thiagofw Programming 3 12-11-2016 07:56 AM
cat << EOF > help , when copy file with $variable to other file dr.x Linux - Server 3 04-25-2015 08:50 AM
[SOLVED] cat > filename << "EOF" syntax explanation bucovaina78 Linux - General 4 12-21-2011 03:50 PM
bash redirection "$ cat << EOF > file" (how does this work) ninmonkeys Linux - General 1 11-09-2004 03:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration