LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script, what does this line do: "cat -- << EOF >${CreateScript_DeployDirs}" (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-what-does-this-line-do-cat-eof-%24%7Bcreatescript_deploydirs%7D-4175681417/)

nkamp 09-02-2020 10:12 AM

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.

pan64 09-02-2020 11:25 AM

cat will write the text between EOFs into the named file. If that was possible (=writing that file).

teckk 09-02-2020 11:33 AM

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


teckk 09-02-2020 11:46 AM

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.

nkamp 09-03-2020 02:19 AM

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


All times are GMT -5. The time now is 05:17 PM.