How to output bash multiline EVAL statement to temp file [duplicate] - linux

This question already has answers here:
How to substitute shell variables in complex text files
(12 answers)
Closed 3 years ago.
Trying to variable replace a templated yaml file.
I'm using eval to take the environment shell variables and replace whats in the file dynamically. I can't figure out how to take the output of this and save to a file.
I just want to take the evaluated output and save to a file.
eval "cat <<EOF
$(<${baseFileName})
EOF"
Exmaple test.yaml
---
value: ${PORT}
Bash environment variable:
PORT=8888
output temp.test.yaml
---
value: 8888
Right now the code will just print the evaluated text to the console.
I've tried.
eval "cat <<EOF
$(<${baseFileName})
EOF" > $newBaseFileName
but no joy. Didn't even create the file.
The reason I'm not using sed is because the file could have unlimited variable decelerations, and I want to replace any value matching a defined bash variable or environment variable. This is part of a template engine. For the life of me I can't remember how I did it before with pure bash.

It didn't work for me but what I did is this
renderTemplate() {
eval "cat <<EOF
$(<${1})
EOF"
}
baseFileName=$(basename $fileName)
templateOutput=`renderTemplate ${baseFileName}`
echo "${templateOutput}"
I'm using this as a temp file anyways so what I'll do is save to variable and then pump that variable in to the command to apply the template as a file. That way it's only ever stored in memory. This is a middleware cli to another cli to add variable replacement to in-memory web hosted files before applying them.
Thanks for your help.

Related

Bash script file as input [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 2 years ago.
i am trying to give an file as input to me my shell script:
#!/bin/bash
file ="$1"
externalprogram "$file"
echo 'unixcommand file '
i am trying to give the path to my file but it says always
cannot open `=/home/username/documents/file' (No such file or directory)
my path is this /home/username/Documents/file
i do this in terminal : ./myscript.sh /home/username/Documents/file
can someone help me please?
When you say
file ="$1"
with a space after "file", you're running something called file with =$1 as an argument. There probably actually is a utility called file. If you want to assign $1 to a variable called file, you don't need the space:
file="$1"
there shouldn't be a space before = in the second line.
file=$1 should be good enough.
Check what shellcheck says about your code:
^-- SC1068: Don't put spaces around the = in assignments (or
quote to make it literal).
You can read more about SC1068 case on its Github
page.
#!/bin/bash
file=$1
code $file
echo "aberto o arquivo ${file} no vscode"
I made this code snippet to demonstrate, I pass a path and it opens the file in vscode

Evaluating variable - output json file contents [duplicate]

This question already has answers here:
File content into unix variable with newlines
(6 answers)
Closed 4 years ago.
In the bash shell, I'm trying to read the json file and load to a variable
eri#xyz:~/Documents/inbound>e1=$(eval echo $(cat ./deploy/request.json))
Upon fetching the output of that variable, I'm seeing -bash - command not found along with the actual contents of the .json file
eri#xyz:~/Documents/inbound>"$e1"
-bash: { type:Pipeline, category:Software, risk:4, short_description:sample short description text, description:sample detailed description text, assignment_group: Services - Retail Services, cmdb_ci:Retail Service, u_version:1.0, start_date:2017-01-04 18:00:00, end_date:2017-01-04 19:00:00, backout_plan:see department for standard backout plan, implementation_plan:sample implementation plan, test_plan:sample text plan, production_system:false }: command not found
Is there a way to suppress the -bash - command not found in the output?.
No need for eval - just e1=$(< ./deploy/request.json) should do the trick. (Thanks to #shellter for the syntax — you don't even need to use cat!)
To show the variable, you want
echo "$e1"
instead of just "$e1". "$e1" by itself on the command line does not print out the value of $e1, unlike many programming-language REPLs. Instead, it tells bash to try to interpret the entire contents of $e1 as the name of a command. It isn't the name of a command, so bash tells you a command by that name cannot be found.

Unix: What does cat by itself do?

I saw the line data=$(cat) in a bash script (just declaring an empty variable) and am mystified as to what that could possibly do.
I read the man pages, but it doesn't have an example or explanation of this. Does this capture stdin or something? Any documentation on this?
EDIT: Specifically how the heck does doing data=$(cat) allow for it to run this hook script?
#!/bin/bash
# Runs all executable pre-commit-* hooks and exits after,
# if any of them was not successful.
#
# Based on
# http://osdir.com/ml/git/2009-01/msg00308.html
data=$(cat)
exitcodes=()
hookname=`basename $0`
# Run each hook, passing through STDIN and storing the exit code.
# We don't want to bail at the first failure, as the user might
# then bypass the hooks without knowing about additional issues.
for hook in $GIT_DIR/hooks/$hookname-*; do
test -x "$hook" || continue
echo "$data" | "$hook"
exitcodes+=($?)
done
https://github.com/henrik/dotfiles/blob/master/git_template/hooks/pre-commit
cat will catenate its input to its output.
In the context of the variable capture you posted, the effect is to assign the statement's (or containing script's) standard input to the variable.
The command substitution $(command) will return the command's output; the assignment will assign the substituted string to the variable; and in the absence of a file name argument, cat will read and print standard input.
The Git hook script you found this in captures the commit data from standard input so that it can be repeatedly piped to each hook script separately. You only get one copy of standard input, so if you need it multiple times, you need to capture it somehow. (I would use a temporary file, and quote all file name variables properly; but keeping the data in a variable is certainly okay, especially if you only expect fairly small amounts of input.)
Doing:
t#t:~# temp=$(cat)
hello how
are you?
t#t:~# echo $temp
hello how are you?
(A single Controld on the line by itself following "are you?" terminates the input.)
As manual says
cat - concatenate files and print on the standard output
Also
cat Copy standard input to standard output.
here, cat will concatenate your STDIN into a single string and assign it to variable temp.
Say your bash script script.sh is:
#!/bin/bash
data=$(cat)
Then, the following commands will store the string STR in the variable data:
echo STR | bash script.sh
bash script.sh < <(echo STR)
bash script.sh <<< STR

Use shell to load in variables to replace placeholders

I have a problem where my config files contents are placed within my deployment script because they get their settings from my setting.sh file. This causes my deployment script to be very large a bloated.
I was wondering if it would be possible in bash to do something like this
setting.sh
USER="Tom"
log.conf
log=/$PLACEHOLDER_USER/full.log
deployment.sh
#!/bin/bash
# Pull in settings file
. ./settings.sh
# Link config to right location
ln -s /home/log.conf /home/logging/log.conf
# Write variables on top of placeholder variables in the file
for $PLACEHOLDER_* in /home/logging/log.conf
do
(Replace $PLACEHOLDER_<VARAIBLE> with $VARIABLE)
done
I want this to work for any variable found in the config file which starts with $placeholder_
This process would allow me to move a generic config file from my repository and then add the proper variables from my setting file on top of the placeholder variables in the config.
I'm stuck on how I can get this to actually work using my deployment.sh.
This small script will read all variable lines from settings.sh and replace the PLACEHOLDER_xxx in file for each. Does this help you?
while IFS== read variable value
do
sed -i "s/\$PLACEHOLDER_$variable/$value/g" file
done < settings.sh
#!/usr/local/env bash
set -x
ln -s /home/log.conf /home/logging/log.conf
while read user
do
usertmp=$(echo "${user}" | sed s'#USER=\"##' \
sed s'#"$##')
user="${usertemp}"
log="${user}"/full.log
done < setting.sh
I don't really understand the rest of what you're trying to do, I will confess, but this will hopefully give you the idea. Use read.

Parsing a variable in shell scripting

I am new to shell scripting just started off.
I have written this script
#!/bin/sh
profile_type= cat /www/data/profile.conf
echo $profile_type
the o/p of this script is
. /tmp/S_panicA1.txt
. /tmp/S_panicA0.txt
away_Def="panicA1 panicA0"
away_Byp=0
away_Sts=$((panicA1+panicA0-away_Byp))
In this i want to get panicA1 panicA0 and 0 and store it in other variable how to do this?
When you want to assign the output of a command to a variable, you use the dollar parenthesis syntax.
foo=$(cat /my/file)
You can also use the backticks syntax.
foo=`cat /my/file`
In your script, you simply run the command cat and assign its result, nothing, to your variable. Hence the output consisting of the content of your file, result of cat, followed by an empty line, result of echo with an empty variable.

Resources