This question already has answers here:
How to check if a string contains a substring in Bash
(29 answers)
Closed 3 years ago.
Output is same, and it always echos need to pull.
If I remove the quotes around $text in if condition it throws the too many arguments error.
var="$(git status -uno)" &&
text="On branch master Your branch is up-to-date with 'origin/master'. nothing to commit (use -u to show untracked files)";
echo $var;
echo $text;
if [ "$var" = "$text" ]; then
echo "Up-to-date"
else
echo "need to pull"
fi
Better do this, =~ for bash regex :
#!/bin/bash
var="$(git status -uno)"
if [[ $var =~ "nothing to commit" ]]; then
echo "Up-to-date"
else
echo "need to pull"
fi
or
#!/bin/bash
var="$(git status -uno)"
if [[ $var == *nothing\ to\ commit* ]]; then
echo "Up-to-date"
else
echo "need to pull"
fi
Warning: bash's regex require more ressources and won't work in other shell!
Simple old fashion
This syntax is POSIX compatible, not bash only!
if LANG=C git status -uno | grep -q up-to-date ; then
echo "Nothing to do"
else
echo "Need to upgrade"
fi
Or testing a variable (posix too)
From this answer to How to check if a string contains a substring in Bash, here is a compatible syntax, working under any standard POSIX shell:
#!/bin/sh
stringContain() { [ -z "${2##*$1*}" ] && { [ -z "$1" ] || [ -n "$2" ] ;} ; }
var=$(git status -uno)
if stringContain "up-to-date" "$var" ;then
echo "Up-to-date"
# Don't do anything
else
echo "need to pull"
# Ask for upgrade, see:
fi
Related
I have the below shell script that i need to echo to a file lets say script.sh from a groovy interface like Jenkinsfile but keep getting compilation errors.
#!/bin/bash
commit_hash=$(git rev-parse HEAD)
parent_hashes=`git rev-list --parents -n 1 $commit_hash`
parent_count=`wc -w <<< $parent_hashes`
if [[ $parent_count -gt 2 ]]
then
p=`git name-rev $parent_hashes | xargs -0 | grep -e '^\S\+ master$'`
if [[ ! -z $p ]]
then
echo "merged branch is master"
exit 0
else
echo "merged branch is anything but master"
exit 2
fi
else
echo "no branch merged"
exit 1
fi
I tried the below :-
sh '''echo '#!/bin/bash
commit_hash=$(git rev-parse HEAD)
parent_hashes=`git rev-list --parents -n 1 $commit_hash`
parent_count=`wc -w <<< $parent_hashes`
if [[ $parent_count -gt 2 ]]
then
p=`git name-rev $parent_hashes | xargs -0 | grep -e '^\S\+ master$'`
if [[ ! -z $p ]]
then
echo "merged branch is master"
exit 0
else
echo "merged branch is anything but master"
exit 2
fi
else
echo "no branch merged"
exit 1
fi' > script.sh'''
I see the shell script has single quotes in a line plus a few back slashes, so not sure why groovy is not allowing normal shell interpolation here.
How do i get to echo the contents of this shell script to a file using groovy. I am trying this out in scripted Jenkinsfile.
You can try using writeFile option to write the content into file, but in your case you have to escape backslash alone in your script. Below should work.
pipeline {
agent any
stages {
stage ("Test") {
steps{
writeFile file:'test.txt', text: '''#!/bin/bash
commit_hash=$(git rev-parse HEAD)
parent_hashes=`git rev-list --parents -n 1 $commit_hash`
parent_count=`wc -w <<< $parent_hashes`
if [[ $parent_count -gt 2 ]]
then
p=`git name-rev $parent_hashes | xargs -0 | grep -e '^\\S\\+ master$'`
if [[ ! -z $p ]]
then
echo "merged branch is master"
exit 0
else
echo "merged branch is anything but master"
exit 2
fi
else
echo "no branch merged"
exit 1
fi'''
}
}
}
}
To write your script into a file use the writeFile step (see here). This will create a file in your workspace from a string.
In a declarative pipeline it looks something like this:
writeFile(file: "fileName", text: "Your Script")
This question already has answers here:
Test if a command outputs an empty string
(13 answers)
Closed 4 years ago.
Some Example:
I've a shellscript where I want to check if a the stdout of a command is empty.
So I can do
if [[ $( whateverbin | wc -c) == 0 ]] ; then
echo no content
fi
But is there no direct command, to check this? Something like :
if whateverbin | checkifstdinisempty ; then
echo no content
fi
You could use the -z conditional expression to test if a string is empty:
if [[ -z $(ls) ]]; then echo "ls returned nothing"; fi
When you run it on an empty result, the branch gets executed:
if [[ -z $(cat non-existing-file) ]]; then echo "there was no result"; fi
Just try to read exactly one character; on no input, read will fail.
if ! whateverbin | IFS= read -n 1; then
echo "No output"
fi
If read fails, the entire pipeline fails, and the ! negates the non-zero exit status so that the entire condition succeeds.
[[ `echo` ]] && echo output found || echo no output
--> no output
[[ `echo something` ]] && echo output found || echo no output
--> output found
With if :
if [ `echo` ] ; then echo ouput found; else echo no output; fi
I am trying to get a pre-commit hook to work, to check the stagged files and read the difference to check for a few strings. If the strings exist, commit has to fail.
#!/bin/bash
#import os
echo "Running pre-commit hook"
checks=os.environ["APPSETTING_DEVPASSWORD"],os.environ["APPSETTING_DEVUSER"],os.environ["APPSETTING_DEVPASS_ELMAH"]
git diff --cached --name-status | while read x file; do
if [ "$x" == 'D' ]; then continue; fi
for word in $checks
do
if egrep $word $file ; then
echo "ERROR: Disallowed expression \"${word}\" in file: ${file}"
exit 1
fi
done
done || exit $?
It still commits the files even though the strings exist in the files. Any guidance would be greatly appreciated. I am fairly new to bash.
It must be something like this:
#!/bin/bash
echo "Running pre-commit hook"
checks=($APPSETTING_DEVPASSWORD $APPSETTING_DEVUSER $APPSETTING_DEVPASS_ELMAH) # create an array
git diff --cached --name-status | while read flag file; do
if [ "$flag" == 'D' ]; then continue; fi
for word in ${checks[#]}
do
if egrep -q "$word" "$file"; then
echo "ERROR: Disallowed expression \"${word}\" in file: ${file}" >&2
exit 1
fi
done
done
get_git_branch(){
local branch__=
git branch &> /dev/null
if [ $? -eq 0 ]; then
branch__=`git branch --no-color | sed -ne 's/^\* \(.*\)$/\1/1p' | tr a-z A-Z`
else
branch__="NORMAL"
fi
echo -n $branch__
}
exit_status(){
local smile__=
if [ $? -eq 0 ]; then
smile__='(*´▽`*)'
else
smile__='(╥﹏╥)'
fi
echo -n $smile__
}
export PS1='[\w]\d\t\$\n\u->(`get_git_branch`)`exit_status`:'
This is PS1 setting in my bashrc,I want to check git branch and exit status in my terminal, get_git_branch works every time PS1 refreshed, but exit_status not, whey exit_status not executed?
It absolutely is executed. However, $? is changed by other code that's run before it -- like get_git_branch.
The best practice here is not to embed code where you want detailed flow control in PS1, but rather to use PROMPT_COMMAND.
get_git_branch(){
local branch
if branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null); then
printf '%s\n' "${branch^^}" # if on bash 3.2, you may need to use tr instead
else
echo "NORMAL"
fi
}
exit_status(){
if (( ${1:-$?} == 0 )); then
printf '%s' '(*´▽`*)'
else
printf '%s' '(╥﹏╥)'
fi
}
build_prompt() {
last_exit_status_=$?
PS1='[\w]\d\t\$\n\u->($(get_git_branch))$(exit_status "$last_exit_status_"):'
}
PROMPT_COMMAND=build_prompt
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
I'm having an issue with a script that I am trying to program. Narrowed down and simplified code and it gives an error that command is not found. If i do "test -f file" in command line it returns nothing, not command not found
PATH=$1
#!/bin/bash
DIR=$1
if [[-f $PATH]]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
exit
Here is the actual more complicated script I'm trying to run
verify()
{
if [[-f $1]]; then
VFY[$2]="f"
echo "$1 is a file"
elif [[-d $1]]
then
VFY[$2]="d"
echo "$1 is a directory"
else
VFY[$2]=0
echo -e "\r"
echo "$1 is neither a file or a directory"
echo -e "\r"
fi
}
Its part of a larger script that can move things around depending on inputs. I've run this in CentOS 6, and FreeBSD, both give the same error "[[-f: Command not found"
Simply add an extra space between [[ and -f, and also before ]].
You will get:
#! /bin/bash
DIR=${1-} # unused in your example
if [[ -f test.sh ]]; then
echo "expression evaluated as true"
else
echo "expression evaluated as false"
fi
exit
and for your function
verify() # file ind
{
local file=$1 ind=$2
if [[ -f "$file" ]]; then
VFY[ind]="f" # no need of $ for ind
echo "$file is a file"
elif [[ -d "$file" ]]; then
VFY[ind]="d"
echo "$file is a directory"
else
VFY[ind]=0
echo -e "\n$file is neither a file or a directory\n"
fi
}