Storing output of MYSQL command in variable in UNIX Bash shell - unix

I am having issue in storing output of MYSQL command in variable in UNIX Bash shell
db_out= $(mysql -u$MASTER_DB_USER -p$MASTER_DB_PASSWD -P$MASTER_DB_PORT -h$MASTER_DB_HOST -D$MASTER_DB_NAME<<Enf
show databases;
Enf)
echo $db_out
I am getting no output in variable $db_out.
Can some body suggest what is missing in above query?
having below output:
db_out= mysql -u$MASTER_DB_USER -p$MASTER_DB_PASSWD -P$MASTER_DB_PORT -h$MASTER_DB_HOST -D$MASTER_DB_NAME --execute "show databases"
+ db_out=
+ mysql -uroot -paxway -P3306 -h10.151.14.248 -Dsentinel --execute 'show databases'
+--------------------+
| Database |
+--------------------+
+--------------------+
| information_schema |
| composer |
| mysql |
| sentinel |
| test |
+--------------------+
echo "$db_out"
+ echo ''

It seems that the output is written to STDERR.
try:
db_out= $(mysql -u$MASTER_DB_USER -p$MASTER_DB_PASSWD -P$MASTER_DB_PORT -h$MASTER_DB_HOST -D$MASTER_DB_NAME 2>&1 <<Enf
show databases;
Enf)
or:
db_out= $( echo "show databases;" | mysql -u$MASTER_DB_USER -p$MASTER_DB_PASSWD -P$MASTER_DB_PORT -h$MASTER_DB_HOST -D$MASTER_DB_NAME 2>&1)

Related

pipe fzf selection to text manipulation to zsh prompt (without executing)

Here's a function that executes a selected alias using fzf:
fa() {
eval $(alias | fzf | cut -d'=' -f2 | sd -p "'" '')
}
The problem is that sometimes you want to add more arguments to an alias rather than immediately execute it. Is this possible?
Solution using zsh:
fa() {
print -z $(alias | fzf | cut -d'=' -f2 | sd -p "'" '')
}

How to filter using jq array of strings which contains a specific value

How do I filter using jq which contains string "edp-api-dev"
{
"serviceArns": [
"arn:aws:ecs:us-east-1:1234:service/splat-dev/abc-api-dev-ecs-abc-api-man-1920299",
"arn:aws:ecs:us-east-1:1234:service/edp-api-dev-ecs-edp-api-man-721g8a7d",
"arn:aws:ecs:us-east-1:1234:service/tsm-frontend-dev-ecs-tsm-frontend-man",
"arn:aws:ecs:us-east-1:1234:service/doc-svc-dev-ecs-doc-svc-man",
"arn:aws:ecs:us-east-1:1234:service/wwk-frontend-dev-ecs-wwk-frontend-man-8fea6a0b",
"arn:aws:ecs:us-east-1:1234:service/xyaz-fsse-ecs-xyaz-fsse-man"
]
}
I tried
aws ecs list-services --cluster splat-dev --profile mfa | jq -r '.serviceArns[] | select( . | contains("edp-api-dev")'
but get syntax error
I forgot the closing parenthesis:
aws ecs list-services --cluster splat-dev --profile mfa |
jq -r '.serviceArns[] | select(contains("edp-api-dev"))'
# ^

tcsh passing a variable inside a shell script

I've defined a variable inside a shell script and I want to use it. For some reason, I cannot pass it into to command line that I need it in.
Here's my script which fails at the last lines
#! /usr//bin/tcsh -f
if ( $# != 2 ) then
echo "Usage: jump_sorter.sh <jump> <field to sort on>"
exit;
endif
set a = `cat $1 | tail -1` #prepares last row for check with loop
set b = $2 #this is the value last row will be checked for
set counter = 0
foreach i ($a)
if ($i == "$b") then
set bingo = $counter
echo "$bingo is the field to print from $a"
endif
set counter = `expr $counter + 1`
end
echo $bingo #this prints the correct value for using in the command below
cat $1 | awk '{print($bingo)}' | sort | uniq -c | sort -nr #but this doesn't work.
#when I use $9 instead of $bingo, it does work.
How can I pass $bingo into the final line correctly, please?
Update: following the accepted answer from Martin Tournoij, the correct way to handle the "$" sign in the command is:
cat $1 | awk "{print("\$"$bingo)}" | sort | uniq -c | sort -nr
The reason it doesn't work is because variables are only substituted inside double quotes ("), not single quotes ('), and you're using single quotes:
cat $1 | awk '{print($bingo)}' | sort | uniq -c | sort -nr
The following should work:
cat $1 | awk "{print($bingo)}" | sort | uniq -c | sort -nr
You also have an error here:
#! /usr//bin/tcsh -f
That should be:
#!/usr/bin/tcsh -f
Note that csh isn't usually recommended for scripting; it has many quirks and lacks some features like functions. Unless you really need to use csh, it's recommended to use a Bourne shell (/bin/sh, bash, zsh) or a scripting language (Python, Ruby, etc.) instead.

tmux bind shortcut to create a pane above or to the left?

Whenever I use tmux split-window -h/v, it creates the new split to the right/bottom, respectively. I want a command that creates the new split on the other side (i.e., to the left/top), but I can't find any simple answer for this anywhere... How can I bind this behavior into a shortcut?
What happens by default:
_______ _______
| | | | |
| * | == split-window -h => | | * |
| | | | |
------- -------
What I want a shortcut for:
_______ _______
| | | | |
| * | == ? => | * | |
| | | | |
------- -------
As from version 2.0, tmux's split-window and join-window understand -b to create the pane to the left or above the target pane.
Split horizontally and place at the left:
tmux split-window -hb
Split vertically and place at the top:
tmux split-window -vb
Update:
From inside tmux you can use } for swapping the panels once you have split the window:
Example for vertical splitting:
Ctrl + B + %
Ctrl + B + }
As above, but remember, tmux has a problem if you run it from PuTTY or from virtual machine. It works properly, but there's sometimes a problem with create, switching and resize panes due to lack of keys recognition.

Need bash script line to log in to WordPress with curl/wget or similar on daily basis with cron

I have an endpoint URL that needs to be hit daily to execute an API call. It requires login. I can't seem to get a cURL or wget command that will successfully log in. I've tried this cURL command:
/usr/bin/curl -L --silent --data
"log=login&pwd=password&ag_login_accept=1&ag_type=login"
https://www.the-url.com 2>&1 | /usr/bin/mail -s "subject"
email#domain.com
but the output is html of the login page, not the api output I get if I manually log in and then go to the url.
I also tried wget:
wget --save-cookies ~/sites/scripts/cookies.txt --keep-session-cookies
--post-data="log=login&pwd=password&ag_login_accept=1&ag_type=login" \
"https://www.the-url.com"
with the same result.
I do this sort of thing:
#!/bin/bash
WPUSR=user_name_goes_here
WPPWD=password_goes_here
COOKIEFILE=`mktemp`
COPT="--load-cookies $COOKIEFILE --save-cookies $COOKIEFILE --keep-session-cookies"
WGET="wget -nv -q ${COPT}"
MSG=`which banner || which figlet || which echo`
function printout() {
links -dump ${1} | grep -v "^ *$" | grep -A 10 "Skip to content"
}
function message() {
$MSG "$1"
}
# login
message 'Login'
LOGIN="log=${WPUSR}${LOGIN}&pwd=${WPPWD}"
LOGIN="${LOGIN}&redirect_to=http://127.0.0.1/wp/?p=1"
${WGET} -O page_01.html --post-data="${LOGIN}" 'http://127.0.0.1/wp/wp-login.php'
printout page_01.html
# show post
message 'View Post'
${WGET} -O page_02.html 'http://127.0.0.1/wp/?p=2'
printout page_02.html
rm "${COOKIEFILE}"
output:
| | ___ __ _(_)_ __
| | / _ \ / _` | | '_ \
| |__| (_) | (_| | | | | |
|_____\___/ \__, |_|_| |_|
|___/
Skip to content
Sitename
Sitename
Just another WordPress site
Posted on 2018-04-11 by jmullee
Hello world!
Welcome to WordPress. This is your first post. Edit or delete it, then
start writing!
One Reply to “Hello world!”
1. A WordPress Commenter says:
2018-04-11 at 16:05
__ ___ ____ _
\ \ / (_) _____ __ | _ \ ___ ___| |_
\ \ / /| |/ _ \ \ /\ / / | |_) / _ \/ __| __|
\ V / | | __/\ V V / | __/ (_) \__ \ |_
\_/ |_|\___| \_/\_/ |_| \___/|___/\__|
Skip to content
Sitename
Sitename
Just another WordPress site
Sample Page
This is an example page. It’s different from a blog post because it will
stay in one place and will show up in your site navigation (in most
themes). Most people start with an About page that introduces them to
potential site visitors. It might say something like this:
Hi there! I’m a bike messenger by day, aspiring actor by night, and this
is my website. I live in Los Angeles, have a great dog named Jack, and I
Alternative suggestion, connect to ssh from a php bash script:
Additional php package needed: php-ssh2
sudo apt install php-ssh2
#!/usr/bin/php
<?php
$connect = ssh2_connect('20.32.66.66.xx', 22);
ssh2_auth_password($connect, 'root', 'PtrDHfutyxxx');
$shell = ssh2_shell($connect, 'xterm');
$stream = ssh2_exec($connect, 'ls -a'); // Example command execute ls
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream_out); // Output command result
// ...
It is a base, fine to use from the shell over a vpn.
To avoid passwords in scripts, and secure it one level up, use crypto key pairs for logging.
http://php.net/manual/en/function.ssh2-publickey-init.php

Resources