Problem with executing only first element into array [duplicate] - arrays

This question already has answers here:
While loop stops reading after the first line in Bash
(5 answers)
Closed 1 year ago.
I thought that my problem is trivial, but I cannot figure out, why my scripts only performing once in array.
I have a jenkins job (bash script). This job gathering hostnames and sends ssh commands, through script, using gathered info:
rm /tmp/hosts
docker exec -t tmgnt_consul_1 consul members -status=alive | grep -v Node | awk '{print $1}' | cut -d : -f1 >> /tmp/hosts
sed -i '/someunnecessaryinfo/d' /tmp/hosts
echo >> /tmp/hosts
shopt -s lastpipe
while IFS= read -r line; do
echo "host is >>$line<<";
url="http://111.111.111.111:8500/v1/catalog/nodes"
term_IP=`curl -s $url | jq -r --arg Node "${line}" '.[] | select(.Node == "'${line}'" )|.Address' --raw-output`
echo $term_IP
sudo bash -x /home/rtm/t_mgnt/check_fw $term_IP
done < /tmp/hosts
Second script:
#!/bin/bash
term_IP=$1
sudo sshpass -p 'some.pass' ssh -o StrictHostKeyChecking=no user#$term_IP "sudo test -d /root/nv9"
if [ $? != 0 ]; then
sudo sshpass -p 'some.pass' \
scp -n -o StrictHostKeyChecking=no -r /home/rtm/t_mgnt/nv9 user#$term_IP:
sudo sshpass -p 'some.pass' \
ssh -n -o StrictHostKeyChecking=no user#$term_IP "sudo mv nv9 /root/"
sudo sshpass -p 'some.pass' \
ssh -n -o StrictHostKeyChecking=no user#$term_IP "sudo dpkg -i /root/nv9/libudev0_175-0ubuntu9_amd64.deb"
sudo sshpass -p 'some.pass' \
ssh -n -o StrictHostKeyChecking=no user#$term_IP "sudo /root/nv9/DetectValidator"
else
sudo sshpass -p 'some.pass' \
ssh -n -o StrictHostKeyChecking=no user#$term_IP "sudo /root/nv9/DetectValidator"
fi
The job is working fine, and returns correct values, but only for the first element of array.
PS - I already searched through this and other sites, and - following answer didn't help me - Shell script while read line loop stops after the first line (already "ssh -n -o").
Perhaps you can point me, what I missed.

Possibly this ssh call eats your input:
sudo sshpass -p 'some.pass' ssh -o StrictHostKeyChecking=no user#$term_IP "sudo test -d /root/nv9"
^^^
Try adding -n.

Related

Retrieve rows in DB corresponding to particular ID using kubectl

I am trying to fetch the no. of rows for a particular ID using kubectl but instead getting some extra data.
Command:
kubectl exec abc-db-0 -n cicd --kubeconfig /root/admin.conf -- bash -c "psql -U postgres -d db -f /tmp/queryInstanceId.sql -v v1=full_test | grep [0-9]"
Actual Output of above command:
Defaulting container name to abc-db.
Use 'kubectl describe pod/abc-db-0 -n cicd' to see all of the containers in this pod.
(0 rows)
Expected Output:
(0 rows)
Could anyone please let me know what I am doing wrong here?
Note:
The first 2 lines always comes when we login to the DB manually but in output I only want (0 rows)
The first two lines are output by kubectl exec because the Pod has multiple containers. It is sort of a warning that it picked the first one, which might not be the one you wanted use.
You can specify the target container in your command (-c containername):
kubectl exec abc-db-0 -n cicd --kubeconfig /root/admin.conf -c abc-db -- bash -c "psql -U postgres -d db -f /tmp/queryInstanceId.sql -v v1=full_test | grep [0-9]"
Or you can redirect the standard error with kubectl ... 2>/dev/null (os specific):
kubectl exec abc-db-0 -n cicd --kubeconfig /root/admin.conf -c -- bash -c "psql -U postgres -d db -f /tmp/queryInstanceId.sql -v v1=full_test | grep [0-9]" 2>/dev/null

Heredoc commands for find . -exec sh {} +

I'm trying to convert a hierarcy of TIFF image files into JPG, and out of boredom, I want to do find and ffmpeg in a single file.
So I set find to invoke sh with the -s flag, like thins:
#!/bin/sh
export IFS=""
find "$#" -iname 'PROC????.tif' -exec sh -s {} + << \EOF
for t ; do
ffmpeg -y -v quiet -i $t -c:v mjpeg ${t%.*}.jpg
rm $t
done
EOF
However, there's just too many files in the directory hierarchy, and find chopped filename array into several small pieces, and sh -s was only succesfully invoked for the first argument chunk.
The question being: how could one feed such in-body command to every sh invocation in the find command?
Update
The tag "heredoc" on the question is intended for receiving answers that do not rely on external file or self-referencing through $0. It is also intended that no filename would go through string-array processing such as padding with NUL-terminator or newline, and can be directly passed as arguments.
The heredoc is being used as the input to find. I think your best bet is to not use a heredoc at all, but just use a string:
#!/bin/sh
find "$#" -iname 'PROC????.tif' -exec sh -c '
for t ; do
ffmpeg -y -v quiet -i "$t" -c:v mjpeg "${t%.*}.jpg" &&
rm "$t"
done
' sh {} +
I am re-writing your code below:-
#!/bin/bash
find "$1" -name "PROC????.tif" > /tmp/imagefile_list.txt
while read filepath
do
path=${filepath%/*}
imgfile=${filepath##*/}
jpgFile=${imgfile%.*}.jpg
cd "$path"
ffmpeg -y -v quiet -i "$imgfile" -c:v mjpeg "$jpgFile"
rm -rf $imgfile
done < /tmp/imagefile_list.txt
IF you don't want to change the current directory you can do it like below:-
#!/bin/bash
find "$1" -name "PROC????.tif" > /tmp/imagefile_list.txt
# If you list down all the .tif file use below command
# find "$1" -name "*.tif" > /tmp/imagefile_list.txt
while read filepath
do
path=${filepath%/*}
imgfile=${filepath##*/}
jpgFile=$path${imgfile%.*}.jpg
ffmpeg -y -v quiet -i "$filepath" -c:v mjpeg "$jpgFile"
rm -rf $imgfile
done < /tmp/imagefile_list.txt
rm -rf /tmp/imagefile_list.txt

Regex for parsing function command line arguments

How i can parse the following function command line:
The separtor is (\s-\w\s) like -c or -d or -n
C:/my app/bin/Reader.dll -n Proc_20ms -c C:/Users/Braun/Desktop/test.csv -t Continue the simulation from the first line of the csv-file -j none -V errors and warnings (default) -d ,
to:
Match1: C:/my app/bin/Reader.dll
Match2: -n
Match3: Proc_20ms
Match4: -c
Match5: C:/Users/Braun/Desktop/test.csv
Match6: -t
Match7: Continue the simulation from the first line of the csv-file
Match8: -j
Match9: none
Match10: -V
Match11: errors and warnings (default)
Match12: -d
Match13: ,
Thanks.
Just put -\w inside a capturing group and then use this regex in re.split function. capturing group is necessary, so that it would keep the delimiter (ie, only the chars present inside the capturing group).
>>> s = 'C:/my app/bin/Reader.dll -n Proc_20ms -c C:/Users/Braun/Desktop/test.csv -t Continue the simulation from the first line of the csv-file -j none -V errors and warnings (default) -d ,'
>>> for i in re.split(r'\s(-\w)\s', s):
print(i)
C:/my app/bin/Reader.dll
-n
Proc_20ms
-c
C:/Users/Braun/Desktop/test.csv
-t
Continue the simulation from the first line of the csv-file
-j
none
-V
errors and warnings (default)
-d
,

What does 'Modify profile to enable bash completion?' mean?

I'm trying to install google app engine. The instructions said to use this command:
$ curl https://sdk.cloud.google.com/ | bash
Now, the installer is asking me this question:
Modify profile to enable bash completion? (Y/n)?
What does that mean?
Edit:
I answered yes, then I was presented with this question:
The Google Cloud SDK installer will now prompt you to update an rc
file to bring the Google Cloud CLIs into your environment.
Enter path to an rc file to update, or leave blank to use
[/Users/7stud/.bash_profile]: /Users/7stud/.bashrc
Backing up [/Users/7stud/.bashrc] to [/Users/7stud/.bashrc.backup].
[/Users/7stud/.bashrc] has been updated. Start a new shell for the
changes to take effect.
The installer added the following to my .bashrc file (Mac OSX 10.6.8):
# The next line updates PATH for the Google Cloud SDK.
source '/Users/7stud/google-cloud-sdk/path.bash.inc'
# The next line enables bash completion for gcloud.
source '/Users/7stud/google-cloud-sdk/completion.bash.inc'
The first script is this:
script_link="$( readlink "$BASH_SOURCE" )" || script_link="$BASH_SOURCE"
apparent_sdk_dir="${script_link%/*}"
if [ "$apparent_sdk_dir" == "$script_link" ]; then
apparent_sdk_dir=.
fi
sdk_dir="$( command cd -P "$apparent_sdk_dir" && pwd -P )"
bin_path="$sdk_dir/bin"
export PATH=$bin_path:$PATH
And the next script is this:
_python_argcomplete() {
local IFS=''
COMPREPLY=( $(IFS="$IFS" COMP_LINE="$COMP_LINE" COMP_POINT="$COMP_POINT" _ARGCOMPLETE_COMP_WORDBREAKS="$COMP_WORDBREAKS" _ARGCOMPLETE=1 "$1" 8>&1 9>&2 1>/dev/null 2>/dev/null) )
if [[ $? != 0 ]]; then
unset COMPREPLY
fi
}
complete -o nospace -o default -F _python_argcomplete "gcloud"
_completer() {
command=$1
name=$2
eval '[[ "$'"${name}"'_COMMANDS" ]] || '"${name}"'_COMMANDS="$('"${command}"')"'
set -- $COMP_LINE
shift
while [[ $1 == -* ]]; do
shift
done
[[ $2 ]] && return
grep -q "${name}\s*$" <<< $COMP_LINE &&
eval 'COMPREPLY=($'"${name}"'_COMMANDS)' &&
return
[[ "$COMP_LINE" == *" " ]] && return
[[ $1 ]] &&
eval 'COMPREPLY=($(echo "$'"${name}"'_COMMANDS" | grep ^'"$1"'))'
}
unset bq_COMMANDS
_bq_completer() {
_completer "CLOUDSDK_COMPONENT_MANAGER_DISABLE_UPDATE_CHECK=1 bq help | grep '^[^ ][^ ]* ' | sed 's/ .*//'" bq
}
unset gcutil_COMMANDS
_gcutil_completer() {
_completer "CLOUDSDK_COMPONENT_MANAGER_DISABLE_UPDATE_CHECK=1 gcutil help | grep -v '^information' | grep '^[a-z]' | sed -e 's/ .*//' -e '/^$/d'" gcutil
}
complete -o default -F _bq_completer bq
complete -o nospace -F _python_argcomplete gsutil
complete -o default -F _gcutil_completer gcutil
I did a little search and what I understand so far is that Bash Completion is bash support for commands auto-complete.
So what GAE seems to be asking for here is to allow that for its command line tools.
It seems git has the same feature as well.

Append child before a particuler child in jqtree

I am using jqTree in our project
http://mbraak.github.io/jqTree/
I have tree structure like this
-A
-B
-C
-C1
-C2
-D
I want to add new child E to A in 2nd position like this
-A
-B
-E
-C
-C1
-C2
-D
How can I do this???
You can use the addNodeAfter function.
See http://mbraak.github.io/jqTree/#functions-addnodeafter

Resources