Bash treating variable expansion as command while passing to other script - bash

a=HDH b=udud c=jsjsj bash secondscript
The command above works. I'd like to save the assignments in a variable, like so:
value="\
a=HDH \
b=udud \
c=ududj \
"
$value bash secondscript
But it gives an error:
test.sh: line 9: a=HDH: command not found
Why? What can I do instead?

bash's taking first item a=HDH as a command, what you need is :
value=(
"a=HDH"
"b=udud"
"c=ududj"
)
env "${value[#]}" bash secondscript

Related

bash script: argument took as command and it's not recognized [duplicate]

This question already has answers here:
Mathematical expression result assigned to a Bash variable
(3 answers)
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 2 years ago.
This is my script:
#!/usr/bin/env bash
set -e
set -o errexit
set -o nounset
# set -o xtrace
input="${1:-}"
table="${2:-}"
length=$(jq length $input)
step=25
for i in {0..${length}..${step}}
do
low_index=$i * ${step}
high_index=$low_index + ${step} - 1
echo $low_index
echo $high_index
jq \
--arg table "$table" \
--arg low_index $low_index \
--arg high_index $high_index \
'{$table: [.[$low_index:$high_index] | {"PutRequest": {"Item": map_values({S: .})}}]}' \
csvjson.json > dynamo_${table}_${low_index}-${high_index}.json
done
This is the context of my directory:
$ ls
csvjson.json splitter.sh
Nevertheless, I'm getting:
$ bash ./splitter.sh csvjson.json socs
./splitter.sh: line 19: csvjson.json: command not found
Consider the line:
low_index=$i * ${step}
and assume that i=0 (which given the code in the question, it will not be, but that is clearly the intent) and step=5. When the shell parses that line, on the first round of word expansions it expands to:
low_index=0 * 5
There's no field splitting necessary, so round 2 is a no-op. The 3rd roundof expansion is pathname expansion, in which the * is expanded to all the names in the current directory, so the line expands to:
low_index=0 cvsjson.json splitter.sh 5
The shell tries to execute that line by looking for a file named cvsjson.json in PATH.

Cannot get value from bash shell output for pg_isready to a variable

I'm trying to get the value for the bash binary call pg_isready to a bash variable
I've tried the following in my script:
#!/bin/bash
haspostgresdb = ${pg_isready -h "ipaddress"}
echo $haspostgresbd
haspostgresdb = ${pg_isready -h ipaddress}
echo $haspostgresbd
haspostgresdb = ${pg_isready -hipaddress}
echo $haspostgresbd
haspostgresdb = ${pg_isready -h"ipaddress"}
echo $haspostgresbd
All return bad substitution as the response. And I did some research and it looks like im doing it correctly
Any suggestions?
Use command substitution and get rid of blanks in the assign command:
haspostgresdb="$(pg_isready -h "ipaddress")"

Delimiter Warning in shell Script with psql

I get a Delimter Error in a Shell Script:
#!/bin/sh
result=`psql -d databasename -t -A <<EOF
SELECT COUNT(*) FROM schema.table
WHERE "column_name_x" = 'specific_value_x'
AND "column_name_y" = 'specific_value_y'
AND ("column_name_z" LIKE 'specific_z%' OR "column_name_za" LIKE 'specific_za%')
;`
EOF
echo $result
#EOF
The result of the Script is fine. But I get two warnings:
./filename.sh: line 13: warning: here-document at line 8 delimited by end-of-file (wanted `EOF')
./filename.sh: line 9: EOF: command not found
What is the problems here? Thank you!
You have the start of your here-doc inside of your command, but the EOF is outside of your command.
result=`psql -d databasename -t -A <<EOF
SELECT COUNT(*) FROM schema.table
WHERE "column_name_x" = 'specific_value_x'
AND "column_name_y" = 'specific_value_y'
AND ("column_name_z" LIKE 'specific_z%' OR "column_name_za" LIKE 'specific_za%')
EOF
`
The ; seems wrong here too (at least it threw an error for me).

Bash script error. What is wrong with this script?

I'm new at bash script writing and I have this error. I have looked everywhere to find an answer with no success. What is wrong with this script?
#!/bin/bash
exec >> /Users/k_herriage/bin/post-gererate.out 2>&1
date
set -x
mynewfile="~/bin/convert_tst.txt"
myfile=fopen($mynewfile,'w+' );
#echo $myfile
fwrite($myfile, "testing");
fclose($myfile);
exit (0)
line 7: syntax error near unexpected token `('
line 7:`myfile = fopen ( '~/bin/convert_tst.txt','w' );'
Few points:
Calling a function in bash does not require parens, it is syntactically equivalent to a command:
do_something arg1 arg2 arg3
There is no need to do open-append-close sequence in bash, it is perfectly doable with a single command:
echo "testing" >> $mynewfile; ##
>> means "append", where if it was >, it would mean "overwrite" or "discard content". (Both will create the file if it didn't exist.)

Error defining variable in bash

I am writing simple housekeeping script. this script contains following line of codes.This is a sample code i have extracted from the actual code since it is a big file.
#!/bin/bash
ARCHIVE_PATH=/product/file
FunctionA(){
ARCHIVE_USER=user1 # archive storage user name (default)
ARCHIVE_GROUP=group1 # archive storage user group (default)
functionB
}
functionB() {
_name_Project="PROJECT1"
_path_Componet1=/product/company/Componet1/Logs/
_path_Component2=/product/company/Componet2/Logs/
##Component1##
archive "$(_name_Project)" "$(_path_Componet1)" "filename1" "file.log"
}
archive(){
_name= $1
_path=$2
_filename=$3
_ignore_filename=$4
_today=`date + '%Y-%m-%d'`
_archive=${ARCHIVE_PATH}/${_name}_$(hostname)_$(_today).tar
if [ -d $_path];then
echo "it is a directory"
fi
}
FunctionA
When i run the above script , i get the following error
#localhost.localdomain[] $ sh testScript.sh
testScript.sh: line 69: _name_Component1: command not found
testScript.sh: line 69: _path_Component2: command not found
date: extra operand `%Y-%m-%d'
Try `date --help' for more information.
testScript.sh: line 86: _today: command not found
it is a directory
Could someone explain me what am i doing wrong here.
I see the line: _today=date + '%Y-%m-%d'
One error I spotted was resolved by removing the space between the + and the ' like so:
_today=date +'%Y-%m-%d'
I don't see where the _name_Component1 and _name_Component2 variables are declared so can't help there :)
Your variable expansions are incorrect -- you're using $() which is for executing a subshell substitution. You want ${}, i.e.:
archive "${_name_Project}" "${_path_Componet1}" "filename1" "file.log"
As for the date error, no space after the +.
a few things... you are using $(variable) when it should be ${variable}
on the date command, make sure there is no space between the + and the format
and you have name= $1, you don't want that space there

Resources