Running Script on container from host - bash

I've made a script to perform an automate update to PHP service in a container.
At some stage i check if the new PHP version is activated with the following commands:
echo ''| update-alternatives --config php | grep $version | grep auto | head -n 1 | tr -d ' \n\r\t '
the "echo" at the beginning canceling the interactive interaction from the 'update-alternatives' command.
the $version variable is an input from the user(OPTARG) and has a default value if not set.
when im trying to save the command output to a variable , the variable is always set to null after executing the command.
i've tried the following ways:
var=$(echo ''| update-alternatives --config php | grep $version | grep auto | head -n 1 | tr -d ' \n\r\t ')
and:
var=`echo ''| update-alternatives --config php | grep $version | grep auto | head -n 1 | tr -d ' \n\r\t '`
The result of the command when im not redirecting the output to a variable is:
0/usr/bin/php8.181automode
Please help

You need to check the failed command piecemeal.
(Note: my system has only one version of php installed so substituting vi for the example below.)
The command
echo '' | update-alternatives --config vi | grep gtk3 | grep auto
reports
{*} 0 /usr/bin/vim.gtk3 50 auto mode
So ... I suggest that your coding doesn't take into account the format for reporting the choices.
Instead of the tr, use
awk '{ for( i=1 ; i < $NF ; i++){ if( index($i,"/") == 1 ){ print $i ; exit } ; } ; }'

Related

Bash Code with while, list and crontab command

I have something like this :
all_scripts=()
for i in "${OS_USER[#]}"; do
list_script=$(crontab -u $i -l | grep -o '[^ ]*\.sh' | grep -o '^[^ ]*' | sort -u)
I want to create a list ( all_scripts) which contain all scripts from all users
For example
if in user 1 crontab there are script1.sh , script2.sh
in user 2 there are scrip3.sh, script4.sh, script5.sh
and in user 3 script6.sh
I want a list with all scripts : script1,sh, script2.sh, script3,...script6.sh
What you presented is incomplete. What you have is almost all correct.
You need to include the action ... to build the list ... within the loop.
You also need to initialize list_script as an array.
You probably want to suppress the "no crontab for ..." messaging.
You also need to be selective regarding which fields are "harvested" from the crontab, so ... ignore comments and ignore parameter definitions.
I recognize that the below may not correctly handle parameter definitions that may include spaces, but it is a good start for what you are looking for. So, the end result would look something like this:
#!/bin/bash
## You may have a specific list instead of this next line.
OS_USER=( $(cut -f1 -d\: /etc/passwd) )
all_scripts=()
list_script=()
for i in ${OS_USER[#]}
do
list_script=$(crontab -u $i -l | grep -v '^#' | awk '{
pos=index($0, $6) ;
$0=substr($0, pos) ;
print NF ;
for( i=1 ; i<=NF ; i++){
if( index($i, "=") == 0 ){
print $i ;
break ;
} ;
} ;
}' | grep -o '[^ ]*\.sh' | grep -o '^[^ ]*' | sort -u )
all_scripts=( ${allscripts[#]} ${list_script[#]} )
done
print ${allscripts[#]}

Java output as variables within same shell script

I'm running JAVA code inside shell script
java -cp ojdbc6.jar:. javaClassName args
Is it possible to do command substitution for java output inside shell
Output of java code is an array:
[{ID:143},{Name:John},{Age:32},{Designation:Enginner},{City:Delhi},{Phone:+123 456 789},{Email:abc#gmai.com}]
I want to declare above array as variables inside the same shell-script where java code runs
ID=${ID}
Name=${Name}
Try
grep -oE '(:[^}]+)' | head -2 | tr -d ':'
Demo :
$read -r Id Name <<<$(echo '[{ID:143},{Name:John},{Age:32},{Designation:Enginner},{City:Delhi},{Phone:+123 456 789},{Email:abc#gmai.com}]' | grep -oE '(:[^}]+)' | head -2 | tr -d ':' )
$echo $Id
143
$echo $Name
John
$

How to add shell script to jenkins pipeline

I have the below shell script:
du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do
DIR=`echo $line | awk '{print$2}'`
Rep=`cat $DIR/repository-config |grep 'project\|repo' | tr '\n' ' '`
Size=`echo $line | awk '{print $1}' `
echo $Size $Rep
done
How can I run it thought Execute shell in Jenkins? I need also to add ssh command to the env (no need for a password).
Note I don't want to connect to the env and run this shell, but directly from Excecue shell box
If I'm not wrong your are using a Freestyle job and not a pipeline job.
Anyway, I think you have to try the following :
ssh -t XXXXX#YYYYY << 'EOF'
du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do\
DIR=echo $line | awk '{print$2}'\
Rep=cat $DIR/repository-config |grep 'project\|repo' | tr '\n' ' '\
Size=echo $line | awk '{print $1}' \
echo $Size $Rep\
done
EOF
I've escaped the code inside your while loop using \, if it's doesn't works you can use ; instead.
If you want help for using a pipeline job, let me know but i might be a bit more complex.

grep -c kills script when no match using set -e

Basic example:
#!/bin/bash
set -e
set -x
NUM_LINES=$(printf "Hello\nHi" | grep -c "How$")
echo "Number of lines: ${NUM_LINES}" # never prints 0
Output:
++ grep -c 'How$'
++ printf 'Hello\nHi'
+ NUM_LINES=0
If there are matches, it prints the correct number of lines. Also grep "How$" | wc -l works instead of using grep -c "How$".
You can suppress grep's exit code by running : when it "fails". : always succeeds.
NUM_LINES=$(printf "Hello\nHi" | grep -c "How$" || :)

bash: how do I concatenate the output of two commands so that I can pipe them to a third?

$ hg status
and
$ hg status --ignored
give very similar outputs. I'd like to concatenate them so I can feed them to awk, as if there were an hg status --all (or svn's svn status --no-ignore)
I'm thinking something like:
$ echo "$(hg status)" "$(hg status --ignored)" | awk ' ( $1 == "?" ) || ( $1 == "I") { print $2 }' | xargs rm -r
to make a 'make very clean indeed' command, but it seems to occasionally leave a file behind, perhaps because a newline goes missing or something.
Use curly braces to group commands:
$ { echo first line; echo second line; } | grep "line"
first line
second line
(Posted as an answer from camh's comment)
You can use a subshell:
( hg status; hg status --ignored ) | awk '( $1 == "?" ) || ( $1 == "I") { print $2 }' | xargs rm -r
You can use the rest of the hg status flags to show what you really want:
hg status -uriamn
That shows unknown files (u), removed files (r), ignored (i), added (a), modified (m) and does so without showing the status prefix.
This works for me:
echo $(a)$(b)
if you add "" you can add delimiters eg.:
echo "$(./gethostname.sh)|($(./getip.sh);"
I use this on Openwrt to broadcast my ip settings:
echo "$( uci get system.#system[0].hostname )|$( ip addr | grep inet | grep br-lan | cut -d ' ' -f 6 | cut -d '/' -f 1 );" | socat - UDP-DATAGRAM:255.255.255.255:4999,broadcast ;

Resources