CQLSH: How to print text on execution of >source 'CommandFile'; - command-line

I am using these functionalities:
Linux>cqlsh
cqlsh>use mydatabase;
cqlsh:mydatabase>source 'myCommands.cqlsh';
...
cqlsh:mydatabase>
Since I am executing a file, I wish to comment some outputs, I would like to find a way to print a sort of echo '' or print "hello"; on the output. Is it possible ?

You could just script this out, piping your split CQL scripts to cqlsh:
#!/bin/sh
echo "SOURCE 'myCommands1.cql'" | cqlsh
echo "hello"
echo "SOURCE 'myCommands2.cql'" | cqlsh
echo "done.."

Related

echo shows command I want to execute

First let me explain that I have very little expertise with bash scripting. I only use it for very simple applications.
My script is used to generate a grep command.
I use the echo command as an interim debug tool. I figure that if I can get the echo command to show the command I want to execute, all I have to do is remove the echo and the quotes and the command inside the echo should do what I want.
Here is my script (called grepper3.sh). Again, I am not an expert at this:
#!/bin/bash
echo "what should I grep?"
read this
echo "grep -Ri \"$this\" > \"$this\""
Here is what happens when I execute:
master#master-Latitude-E6440:~$ ./grepper3.sh
what should I grep?
all that
grep -Ri "all that" > "all that"
The grep command being echoed by the code is exactly what I want. But when I remove the echo and the surrounding double quotes:
was: echo "grep -Ri \"$this\" > \"$this\""
changed to: grep -Ri \"$this\" > \"$this\"
I get this:
master#master-Latitude-E6440:~$ ./grepper3.sh
what should I grep?
all that
./grepper3.sh: line 5: \"$this\": ambiguous redirect
I'm guessing that there is a simple fix, but I can't figure it out.
You can add . to recursive search.
#!/bin/bash
echo "what should I grep?"
read this
grep -Ri $this > $this .
All you need to to is to change the quotation marks to backticks.
Your new code would be:
#!/bin/bash
echo "what should I grep?"
read this
echo `grep -Ri \"$this\" > \"$this\"`
Here is what I finally got to work. Thanks for all of your help!
#!/bin/bash
echo "what should I grep?"
read this
DEST="/home/master/results/$this"
grep -Ri "$this" > "$DEST"

pipe both, stdout and stderr in the fish shell

I know this has been an issue for a while and I found a lot of discussion about it, however I didn't get which would be finally a way to get it done: pipe both, stdout and stderr. In bash, this would be simply:
cmd 2>&1 | cmd2
That syntax works in fish too. A demo:
$ function cmd1
echo "this is stdout"
echo "this is stderr" >&2
end
$ function cmd2
rev
end
$ cmd1 | cmd2
this is stderr
tuodts si siht
$ cmd1 &| cmd2
rredts si siht
tuodts si siht
Docs: https://fishshell.com/docs/current/language.html#redirects
There's also a handy shortcut, per these docs
&>
Here's the relevant quote (emphasis and white space, mine):
As a convenience, the redirection &> can be used to direct both stdout
and stderr to the same destination. For example:
echo hello &> all_output.txt
redirects both stdout and stderr to the file
all_output.txt. This is equivalent to echo hello > all_output.txt 2>&1.

Pulling hostname from TNS entry

I am working on a script that will need to determine which node a db being used by a local app is running on. I've been trying to use this as a chance to force myself to learn awk/sed and have a test script to test the statements. It's working off a copy of the tnsnames.ora file I have moved to the home folder the script is located in.
Here is a valid tnsnames.ora stanza:
(
DESCRIPTION = (
ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP) (Host=iplab)(Port=1521))
)
(CONNECT_DATA=(SID=spurs1))
)
After doing some research and getting the awk expression to pull the tns entry to $host I came up with the below script but it doesn't seem to work.
#!/bin/ksh
db=spurs
host=$(awk -v db=$db "/${db}/ {for(i=1; i<=5; i++) {getline; print}}" tnsnames.ora)
echo $host
host= $host | sed 's/Host\s=\s\([a-z]+[0-9]?\)/\1/'
echo $host
When I run it the awk statement I get the following:
(DESCRIPTION = (ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP) (Host=hostname)(Port=1521))) (CONNECT_DATA=(SID=spurs1)) )
./tns.ksh: line 6: (DESCRIPTION: not found
(DESCRIPTION = (ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP) (Host=hostname)(Port=1521))) (CONNECT_DATA=(SID=spurs1)) )
From what I have seen reading tutorials and forums I think sed is setup correctly and should be setting $host to one or more lowercase letters followed by 0 or 1 numbers after Host = . Since (DESCRIPTION is the start of $host before and after the sed statement I'm not sure how it isn't finding it, an
This worked for me:
tnsping $db | grep HOST | cut -d\ -f 14 | sed 's/).*//g'
On my system I can use this to get the host as long as the host name doesn't have an equals sign (or the actual literal word HOST in the name of the host):
echo $TNS_STRING | sed 's/.HOST//g' | sed 's/).//g' | sed 's/=//g' | sed 's/\s*//g'
Your value for $host is likely a multiline value, so you need to quote it anyplace you use it, i.e.
host=$(awk -v db=$db "/${db}/ {for(i=1; i<=5; i++) {getline; print}}" tnsnames.ora)
echo "$host"
You also need to capture the output (using command-substitution) via $(...)
host=$(echo "$host" | sed 's/Host\s=\s\([a-z]+[0-9]?\)/\1/')
echo "$host"
(and echo it), so it can be processed by sed
Revise
host=$(echo $host | sed 's/.*Host=//; s/).*$//)
echo "$host"
I've switched back to just $host, without the dbl-quotes, as you don't want the linebreaks in the data. Now it is all one big string, and the regex, strips every upto host=, and then strips everything after the first remaining ) char.
If you still get error messages, I don't have access to a tnsnames.ora record, so please edit your query to include a valid record.
I hope this helps.
you may be better relying on the output of tnsping instead of parsing the file: tnsping appears to emit the description on one line:
host=$(
tnsping $db | while read line; do
if [[ "$line" == *HOST* ]]; then
s=${line#*HOST=}; s=${s%%)*}; echo "$s"; break
fi
done
)
This might work for you:
db=spurs
host=$(sed '/^(/,/^)/!d;/^(/{h;d};H;/^)/!d;g;/'"$db"'/!d;s/.*Host=\([^)]*\).*/\1/' tnsnames.ora)
Tested Code:
OIFS=$IFS;
IFS="(";
tns=`tnsping TNS_ALIAS`
tns_arr=($tns);
tns_info=(`(for ((i=0; i<${#tns_arr[#]}; ++i)); do echo "${tns_arr[$i]/)/}"; done)| grep 'HOST\|PORT'|sed 's/)//g'|sed 's/ //g'`)
for ((i=0; i<${#tns_info[#]}; ++i)); do eval "export ${tns_info[$i]}"; done
echo "host:" $HOST
echo "port:" $PORT
IFS=$OIFS;

Linux Bourne shell substitution issue

I'm trying to use substitution in a BS with built data namespace within a (very) small embedded busybox (no man, 60 cmds all in all), but I can't echo the data as soon as there are more than 2 data echoed :
this is OK :
a=$(echo -e ${smtp_0} ${smtp_4})
echo $a
# returns: "0 4" as expected, also all individually printed datas are echoed as expected
this does not echo expected datas whatever data is:
b=$(echo -e ${smtp_0} ${smtp_4} ${smtp_5})
echo $b
# returns: "54" , same with double-quotes (Nok, it should return "0 4 5")
Datas are built like this :
"data file sample"
val0=1
val1=1
...
Reading datas shell:
#!/bin/sh
x=0
while read line
do
# fetch values, removing blank and commented lines, eg keeping only lines starting with data namespace
formatted_line=$(echo $line | sed -e "/^[^a-z].*$/d" | cut -d= -f2)
# store file's value into a data array-like
if [ ! -z $formatted_line ];then
eval "`echo $x | sed -e 's/.*/smtp_&=$formatted_line/'`"
x=$(($x+1))
fi
done < $DATA_FILE
# Then try echoing datas...
# ... see above ...
EDITED:
So it does look like there is nthg mistaken in there but the data file EOL misleading the concatenation of the builtin data. I close the point and thx to Dennis helping getting this headhache fixed.
To preserve whitespace, variables should be quoted:
b=$(echo -e "${smtp_0} ${smtp_4} ${smtp_5}")
echo "$b"
However, why are you using echo?
b="${smtp_0} ${smtp_4} ${smtp_5}"
echo "$b"
Also, you should use indenting in your code (or if you are then you should retain it when posting questions).

how to trim wipe output with sed?

i want to trim an output of wipe command with sed.
i try to use this one:
wipe -vx7 /dev/sdb 2>&1 | sed -u 's/.*\ \([0-9]\+\).*/\1/g'
but it don't work for some reason.
when i use echo & sed to print the output of wipe command it works!
echo "/dev/sdb: 10%" | sed -u 's/.*\ \([0-9]\+\).*/\1/g'
what i'm doing wrong?
Thanks!
That looks like a progress indicator. They are often output directly to the tty instead of to stdout or stderr. You may be able to use the expect script called unbuffer (source) or some other method to create a pseudo tty. Be aware that there will probably be more junk such as \r, etc., that you may need to filter out.
Demonstration:
$ cat foo
#!/bin/sh
echo hello > /dev/tty
$ a=$(./foo)
hello
$ echo $a
$ a=$(unbuffer ./foo)
$ echo $a
hello