How to append the variable to linux command and then execute the final linux command($cmd: qic-run -t /mnt/etc/ -d 1000) using ksh - shell

teststr="-t /mnt/etc/ -d 1000"
echo $teststr
cmd="qic-run $teststr"
echo $cmd
qic-run -t /mnt/etc/ -d 1000

For a single/simple command:
args=(-t /mnt/etc/ -d 1000)
qic-run "${args[#]}"

Related

Is it possible to put value from linux pipe into curl querystring param?

I have a numeric output from command, let's say:
sh -c 'exit 1' ; echo $?
or
bc <<< "1 + 1"
And I need to send it in GET request via curl, like http://example.com/?value=1
I've tried this:
sh -c 'exit 1' ; echo $? | curl -G -d #- http://example.com/
but it just got param with a name 1 and empty value.
I know I can do something like:
result=`sh -c 'exit 129' ; echo $?` | curl -G -d value=${result} http://example.com
but I'd like to keep the first part of command unchanged and modify only part after pipe. Is it possible?
One possible solution I found:
sh -c 'exit 129'; echo $? | xargs -I '{}' curl -G "http://example.com?value={}";

How to use bash variables in Jenkins multi-line shell script

I have unsuccessfully tried to use bash variables in Jenkins pipeline.
My first attempt
sh """#!/bin/bash
for file in *.map; do
filename=`basename $file .map`
echo "##### uploading ${$filename}"
curl -X POST ${SERVER_URL}/assets/v1/sourcemaps \
-F service_name="${SERVICE_NAME}" \
-F service_version="${revision}" \
-F bundle_filepath="${main_url}${filename}" \
-F sourcemap="#${filename}.map" &
done
wait
"""
Resulted in exception: MissingPropertyException: No such property: file
The second attempt, after seeing this answer https://stackoverflow.com/a/35047530/9590251
sh """#!/bin/bash
for file in *.map; do
filename=`basename \$file .map`
echo "##### uploading \$filename"
curl -X POST ${SERVER_URL}/assets/v1/sourcemaps \
-F service_name="${SERVICE_NAME}" \
-F service_version="${revision}" \
-F bundle_filepath="${main_url}\$filename" \
-F sourcemap="#\$filename.map" &
done
wait
"""
Simply omitted bash variables. So $filename was empty.
How do I need to property encode bash variables in this scenario?
Try this:
sh """#!/bin/bash
set -x
for file in *.map; do
filename="\$(basename "\$file" .map)"
echo "Uploading \$filename"
curl -X POST "${SERVER_URL}/assets/v1/sourcemaps" \
-F service_name="${SERVICE_NAME}" \
-F service_version="${revision}" \
-F bundle_filepath="${main_url}\$filename" \
-F sourcemap="#\${filename}.map" &
done
wait
"""

Environment variables not defined in SSH AuthorizedKeysCommand (Docker)

I'm trying to make the private key SSH connection with LDAP.
/etc/ssh/sshd_config
AuthorizedKeysCommand /etc/ldap_ssh_authorized_keys.sh
AuthorizedKeysCommandUser nobody
Script to get public keys from LDAP server
/etc/ldap_ssh_authorized_keys.sh
#!/bin/bash
USERSLIST=$( ldapsearch -x -D "${LDAP_USER}" -w "${LDAP_PASSWORD}" -H $LDAP_URI -b "${LDAP_BASEDN}" -s sub '(objectClass=posixAccount)' -u 'uid' \
grep '^uid:' | sed -n '/^ /{H;d};/uid:/x;$g;s/\n *//g;s/uid: //gp' \
)
while IFS= read -r line; do
exists=$(ldapsearch -x -D "${LDAP_USER}" -w "${LDAP_PASSWORD}" -H $LDAP_URI -b "${LDAP_BASEDN}" \
-s sub "(&(objectClass=posixGroup)(cn=sysadmin)(memberUid=${line}))" | grep "^# numEntries:")
if [[ ! -z $exists ]]
then
ldapsearch -x -D "${LDAP_USER}" -w "${LDAP_PASSWORD}" -H $LDAP_URI -b "${LDAP_BASEDN}" \
-s sub "(&(objectClass=posixAccount)(uid=${line}))" \
-u 'sshPublicKey' \
| sed -n '/^ /{H;d};/sshPublicKey:/x;$g;s/\n *//g;s/sshPublicKey: //gp'
echo -e "";
fi;
done <<< "$USERSLIST"
When I'm running script with /bin/bash it's working well and return my public keys.
All environment variables defined normally.
LDAP_URI
LDAP_BASEDN
LDAP_USER
LDAP_PASSWORD
The script also running normally when trying to make an SSH connection. But environment variables not available.
I'm trying also with AuthorizedKeysCommandUser as root. But nothing changed.
I solved this problem by getting the environment variables from /proc/1/environ.
Reference

command not executing when called from a case statement in bash

I'm stuck and trying to troubleshoot why the command within the case statement will not execute within this bash script.
If I copy and paste the command into the command line and hardcode the value it will work.
Example:
psql -h mycluster-1.us-east-1.redshift.amazonaws.com -U masteruser -d dev -p 5439 -v v1="foo_bar" -f getTableDetails.sql
In the script, I encased the command within parentheses for possible whitespace issues:
#!/usr/local/bin/bash
set -e
set -x
cluster_to_endpoint() {
case "$1" in
c01)
RUNCMD=$(psql -h mycluster-1.us-east-1.redshift.amazonaws.com -U masteruser -d dev -p 5439 -v v1="$NAME" -f getTableDetails.sql)
;;
c02)
RUNCMD=$(psql -h mycluster-2.us-east-1.redshift.amazonaws.com -U masteruser -d dev -p 5439 -v v1="$NAME" -f getTableDetails.sql)
;;
esac
}
while getopts ":c:n:" opt; do
case $opt in
c) cluster="$OPTARG";;
n) name="$OPTARG";;
*) usage
exit 1
;;
esac
done
I am executing the script like so: ./myscript.sh -c c01 -n foo_bar
I've also tried just removing the () as well:
c01)
psql -h mycluster-2.us-east-1.redshift.amazonaws.com -U masteruser -d dev -p 5439 -v v1="$NAME" -f getTableDetails.sql;;
It looks like the variables are set.
$ ./myscript.sh -c c01 -n foo_bar
+ getopts :c:n: opt
+ case $opt in
+ cluster=c01
+ getopts :c:n: opt
+ case $opt in
+ name=foo_bar
+ getopts :c:n: opt
So why isn't the command executing? Thanks.
The command that isn't executing is inside a function, cluster_to_endpoint, that's never called.

bsub post processing script

I was writing a script for submitting parallel jobs in cluster using bsub command
while read p; do
cd $(echo $p | tr -d '\r')
echo Submitting test: $p
bsub -P <project> -Jd <job desc> -o lsf.log "sh ./run_test.sh &> $log"
cd - &> /dev/null
done < $filename
How can I compile the results at the end of all test runs?
How about using something like this?
while read p; do
cd "$(echo "$p" | tr -d '\r')"
echo "Submitting test: $p"
bsub -P <project> -Jd <job desc> -o lsf.log \
"sh ./run_test.sh &> '$log' && cat '$log' >> /path/to/combined/log"
cd - &> /dev/null
done < "$filename"
When each job finishes successfully, the output file is concatenated with the rest.

Resources