Issue with echo in shell script - shell

getting issue with echo output.
line=table_name
echo "SELECT CASE WHEN FORMAT_TYPE LIKE '%character%' THEN 'replace(replace(replace('||ATTNAME||',''\'',''\\''),''"'',''\"''),''|'',''\|'') as '||ATTNAME||',' ELSE ATTNAME||',' END FROM _V_RELATION_COLUMN WHERE NAME = '$line' ORDER BY ATTNUM;"
Output I am looking for something like below:
SELECT CASE WHEN FORMAT_TYPE LIKE '%character%'
THEN 'REPLACE(REPLACE(REPLACE('||ATTNAME||',''\'',''\\''),''"'',''\"''),''|'',''\|'') AS '||ATTNAME||','
ELSE ATTNAME||','
END
FROM _V_RELATION_COLUMN WHERE NAME IN ('table_name')
ORDER BY ATTNUM;
But getting there error as
-bash: syntax error near unexpected token `)'

You need to escape the double quotes in your string
Try
echo "SELECT CASE WHEN FORMAT_TYPE LIKE '%character%' THEN 'replace(replace(replace('||ATTNAME||',''\'',''\\''),''\"'',''\\\"''),''|'',''\|'') as '||ATTNAME||',' ELSE ATTNAME||',' END FROM _V_RELATION_COLUMN WHERE NAME = '$line' ORDER BY ATTNUM;"
Note that to print an escape you have to escape the escape.
e.g
If you want \"
You need to type \\\"

Related

Returning a filename from a ksh function gives a 'bad number' error

I have a shell script with a function and when I call the function I am getting a bad number error. In my function I am getting a filename from the database and returning it. The return gives the 'bad number' error. Details below:
call to GET_MYFILE() :
GET_MYFILE $jobid
--$job id has the correct number in it
GET_MYFILE()
{
echo "job id input parameter is : " $1
curfile=`sqlplus -s /#username<< EOF
set feed off heading off verify off serveroutput off
select my_file_name
from my_table_name
where jobid=$1;
exit;
EOF`
echo "curfile is : " $curfile
return $curfile
}
-- echo "curfile is : " $curfile - displays correct filename
--return $curfile gives bad number error.
In the Korn Shell syntax, the return statement syntax offers only two variants:
return (and in this case, the returned number is the exit code of the most recently executed command in the function).
return n (where n is a number between 0 and 255)
In either case, the caller can access the numeric value returned via the $? pseudovariable in an assignment or conditional statement.
If you try to return a filename then you should expect "bad number", because a filename string cannot usually be interpreted as a number.

Why am I getting a leading sing quote in this echo?

I am trying to debug a bash shell script where I am trying to surround a string/variable with single quotes. I am seeing the following results and am stumped on how to debug this. It obviously has something to do with the content of the variable. I thought the variable may be an array hence some of the echo statements. IN_JSON is being constructed via calls to "jq" to construct some JSON.
echo "IN_JSON = ${IN_JSON}"
echo "IN_JSON = ${IN_JSON[*]}"
echo "IN_JSON = '${IN_JSON[*]}'"
echo "IN_JSON = '" ${IN_JSON} "'"
echo "${#IN_JSON[#]}"
Output:
IN_JSON = {"name":"RX-CLAIM-FILLED"}
IN_JSON = {"name":"RX-CLAIM-FILLED"}
'N_JSON = '{"name":"RX-CLAIM-FILLED"}
'_JSON = ' {"name":"RX-CLAIM-FILLED"}
1
What's going on here and how do I troubleshoot this? It obviously has something to do with the contents of IN_JSON, but I'm not sure why or what is going on here.
The expansion of ${IN_JSON[*]} contains a carriage return character that resets the position of the cursor to beginning of the line, so that the next character ' is printed on beginning of the line.
Most probably, you want to run your file via dos2unix.

Echo printing variables in a completely wrong order

I am trying to create a string with a query that will be save / send to another location, this string contains different variables.
The issue that I am having is that the echo of the variables are completely upside down and mix.
See code below:
tokenID=$(docker exec -ti $dockerContainerID /bin/sh -c "cat /tempdir/tokenfile.txt")
serverName="asdasd"
attQuery="$tokenID $serverName"
agentRegQuery="$./opt/mule/bin/amc_setup -H $attQuery"
echo TOKEN ID $tokenID
echo SERVER NAME $serverName
echo $attQuery
echo $agentRegQuery
Find below the output I am receiving:
TOKEN ID 29a6966f-fa0e-4f08-87eb-418722872d80---46407
SERVER NAME asdasd
asdasdf-fa0e-4f08-87eb-418722872d80---46407
asdasdmule/bin/amc_setup -H 29a6966f-fa0e-4f08-87eb-418722872d80---46407
There's a carriage return character at the end of the tokenID variable, probably because /tempdir/tokenfile.txt is in DOS/Windows format (lines end with carriage return+linefeed), not unix (lines end with just linefeed). When you print tokenID by itself, it looks ok, but if you print something else after that on the same line, it winds up overwriting the first part of the line. So when you print $attQuery, it prints this:
29a6966f-fa0e-4f08-87eb-418722872d80---46407[carriage return]
asdasd
...but with the second line printed on top of the first, so it comes out as:
asdasdf-fa0e-4f08-87eb-418722872d80---46407
The solution is to either convert the file to unix format (dos2unix will do this if you have it), or remove the carriage return in your script. You can do it like this:
tokenID=$(docker exec -ti $dockerContainerID /bin/sh -c "cat /tempdir/tokenfile.txt" | tr -d '\r')
I think everything works as it should
echo TOKEN ID $tokenID -> TOKEN ID 29a6966f-fa0e-4f08-87eb-418722872d80---46407
echo SERVER NAME $serverName -> SERVER NAME asdasd
echo $attQuery -> asdasdf-fa0e-4f08-87eb-418722872d80---46407
echo $agentRegQuery -> asdasdmule/bin/amc_setup -H 29a6966f-fa0e-4f08-87eb-418722872d80---46407
Why do you think something is wron here?
Best regards, Georg

Syntax error at line 1 : `(' is not expected

As I'm new to Unix, can someone help why I get this error?
Error: 0403-057 Syntax error at line 1 : `(' is not expected
Unix server used: AIX servname 1 6 00F635064C00
Script used (to send email alert if day before yesterday source files didn't arrive):
#!/usr/bin/ksh
count=$(sqlplus $PROD_DB #select count(*) from file_audit where (file_name like '%abc%' or file_name like '%dce%') and substr(file_name,17,8)=to_char(to_date(sysdate-2,'DD/MM/YY'), 'yyyymmdd') > asa_file_count.log)
daybefore=`TZ=aaa48 date +%d-%m-%Y`
if [[ $count -lt 20 ]]
then
echo "Alert - Source files are yet to be received for date: $daybefore" | mail -s "Alert : Source data files missing" s#g.com
fi
Parentheses are special to the shell. Your SQL script contains parentheses you don't want the shell to process. However, the shell processes all non-quoted parentheses. Therefore, you can use quotes to prevent the parentheses in your SQL from being interpreted by the shell:
count=$(sqlplus $PROD_DB "#select count(*) from file_audit where (file_name like '%abc%' or file_name like '%dce%') and substr(file_name,17,8)=to_char(to_date(sysdate-2,'DD/MM/YY'), 'yyyymmdd')" > asa_file_count.log)
# ^ and similarly, a closing quote at the end, just before ">asa_file..." .
Now, there is a second issue: you have
count=$(sqlplus ... > asa_file_count.log)
However, I think this means count will always be empty, since the count will go into asa_file_count.log and will not be available to be captured with $(). I believe removing the >asa_file_count.log will probably do what you want:
count=$(sqlplus "$PROD_DB" "<your query>")
(I also put double-quotes around $PROD_DB just in case PROD_DB's value contains any spaces.)

Why doesn't bash recognize the case option with a hyphen/dash?

I'm trying to use a case statement to determine if I have a legal command. It looks something like this:
function commandTest {
case $1 in
–score) echo "something";;
*) echo "unknown";;
esac
}
Now if I use the function like this, it doesn't work, because case doesn't recognize the string correctly although it is identical.
$ commandTest "-score"
unknown
What am I doing wrong here?
As posted, your sample code has an en-dash (Unicode U+2013) in front of score, instead of a minus sign (ASCII 0x2D), which is preventing bash from matching the string -score
Switch:
–score) echo "something"
to:
-score) echo "something"

Resources