I need to compare a string, but I can't. basically I'm retrieving the last pipeline execution status and need to do something according to it, this is my script code :
- |
LAST_EXEC=$(curl --header "PRIVATE-TOKEN: XXXXXXX" "https://xxxxx" | jq -r .[1].status)
- echo "content of LAST_EXEC=${LAST_EXEC}"
- >
if ["$LAST_EXEC" == "success"]; then
echo "we have a success"
else
echo "we do NOT have a success"
fi
I've comfirmed that I have "success" in LAST_EXEC (with the echo just before the if) but the if always go into "we do NOT have success"
I tried with
if ["$LAST_EXEC" == "success"]
if [$LAST_EXEC == success]
if [$LAST_EXEC == "success"]
if ["$LAST_EXEC" == success]
if ["${LAST_EXEC}" == "success"]
if [${LAST_EXEC} == success]
if [${LAST_EXEC} == "success"]
if ["${LAST_EXEC}" == success]
the comparaison never works, (and I don't understand why we might need quote).
Would anyone have a solution for this trivial issue?
Thanks.
this one is working if [ $LAST_EXEC == "success" ] the difference is the spaces
[$LAST_EXEC == "success"] will not work
[ $LAST_EXEC == "success" ] will work
Even the spaces around the == are important, if you write [ $LAST_EXEC=="success" ] it will ALWAYS be true
Related
I am trying to accept user input of yes or no to a question and depending on the answer read back the value of my variable. I can never get commands attached to variables to work or my if statements to accept yes or no. It just keeps going to "not a valid answer".
Please let me know how to actually get those to work in bash script. I keep lookingup different things to try and nothing seems to work.
Here is what I have now:
yesdebug='echo "Will run in debug mode"'
nodebug='echo "Will not run in debug mode"'
echo "Would you like to run script in debug mode? (yes or no)"
read yesorno
if [$yesorno == 'yes']; then
$yesdebug
elif [$yesorno == 'no']; then
$nodebug
else
echo "Not a valid answer."
exit 1
fi
There are several problems with your code:
Failure to put spaces around [ (which is a command name) and ] (which is a mandatory but functionless argument to ]).
Treating the contents of a variable as a command; use functions instead.
yesdebug () { echo "Will run in debug mode"; }
nodebug () { echo "Will not run in debug mode"; }
echo "Would you like to run script in debug mode? (yes or no)"
read yesorno
if [ "$yesorno" = yes ]; then
yesdebug
elif [ "$yesorno" = no ]; then
nodebug
else
echo "Not a valid answer."
exit 1
fi
Following is my csv file
Contact Id,Customer Code,Billing Account Code,Bank BSB,Bank ID
2222222220,2222222222222220,100,084004,fjksanfjkdsksdnfnkjsnQ==
3333333330,3333333333333330,100,084789,sklsnfksnkdfnkgndfkjgn==
and this is the code I'm using to ignore header & any row which has no data in any of 5 columns
while IFS=',' read cont_id cust_code bac bsb bid
do
if [ "$cont_id" == "" ] || [ "$cust_code" == "" ] || [ "$bac" == "" ] || [ "$bsb" == "" ] || [ "$bid" == "" ]; then
echo $cont_id,$cust_code,$bac,$bsb,$bid >> $SOURCE_DIR/dummyRejectedRecords.csv
elif [ "$cont_id" == "Customer Code" ] && [ "$cust_code" == "Customer" ] && [ "$bac" == "Billing Account Code" ] && [ "$bsb" == "Bank BSB" ] &&[ "$bid" == "Bank ID" ]; then
echo $cont_id,$cust_code,$bac,$bsb,$bid >> $SOURCE_DIR/dummyRejectedRecords.csv
else
echo "Contact_Id = '"$cont_id"'"
echo "Customer_Code = '"$cust_code"'"
echo "Billing_Account_Code = '"$bac"'"
echo "Bank_ID = '"$bsb"'"
echo "Bank_BSB = '"$bid"'"
echo ""
#ADD YOUR PROCEDURE HERE
fi
done < $SOURCE_DIR/dummy.csv
The problem is that 1st row is not being ignored
and this is being appended to 1x1 value of csv Customer Code = 'Customer Code'
even if header is ignored the 1st value of next row is appended with 
Could someone help me here (without using awk command)
Thanks a ton in advance
The first thing that comes to mind is to use a simple control variable to skip the first iteration, provided that the file always contains a header that has to be ignored as the first line.
Add this before the first if statement inside the loop:
if [ -z "$HEADER_DONE" ]; then
HEADER_DONE=1
continue
fi
Completely forgot how to do it correctly, and probably forgot the correct terms as well.
Long inline input (I think this is what it's called), using the Teradata bteq as an example client reading the stdin (same as Oracle sql*plus or Sybase isql etc) - so this could be pretty much anything.
bteq <<!
select sql_column1
`if [ "$mode" == "mode1" ]; then`
, sql_column2
`fi`
, sql_column3
from table1
;
!
Here if the mode is "mode1" - I output 3 sql_columns, otherwise two. Now, imagine this is a very very large input, so these conditional manipulations can be very handy.
I'm pretty sure I have done this before, but covid-19 completely flushed my memory.
With this syntax I'm getting: syntax error at line xx: `then' unmatched.
How do I do this right, and what are the correct Unix terms for what I called here as a) inline input; and b) inline condition?
If your desire is to end up with a multi-line SQL statement, then you don't need a "here document" in this specific case because strings can span lines. You can do it easily like this, so long as your text doesn't have embedded quotes.
if [ "$mode" == "mode1" ]; then
col2="
, sql_column2"
else
col2=""
fi
stm="select sql_column1$col2
, sql_column3
from table1
;"
echo "stm='$stm'"
If you don't need a multi-line SQL statement, then the code is simpler.
You can solve your problem on a lot of ways. How can someone read your code after mixing bteq, SQL, Bash and special tests? I think the next approach might help:
I started with replacing bteq by cat for testing.
You can replace your test with
[[ "${mode}" == "mode1" ]] && echo ", sql_column2"
Using this in your here document (using $() and not backtics) results in
cat << END
select sql_column1
$( [[ "${mode}" == "mode1" ]] && echo ", sql_column2")
, sql_column3
from table1
;
EOF
This is not much better for the readers eyes. Now what? Make a function!
mode1_column() {
[[ "${mode}" == "mode1" ]] && echo ", sql_column2"
}
cat << EOF
select sql_column1
$(mode1_column)
, sql_column3
from table1
;
EOF
Looks like either Jeff's revision, or this one, if the "here document" and multiple conditional inclusions are too long to want to break the logic:
bteq <<!
select sql_column1
`if [ "$mode" == "mode1" ]; then
echo \"
, sql_column2
\"
fi`
, sql_column3
from table1
;
!
I am using a nested function to partition and making the filesystem for drives attached to a new Linux box.
I am having a strange issue trying to break out of all loops.
I am keeping track of the nested loop index and using "break n".
When the user replies "n" to the question "Do you have any additional drives to partition?" i expect to break out of all nested loops and continue with the script, but what happens is that the question gets asked again.
Can you help me figure this out?
INIT_STARTED=0
chooseDisks()
{
INIT_STARTED=$((INIT_STARTED+1))
# Choosing which drive to work on
read -p "Please type the name of the disk you want to partition: " DISK
while true; do
read -p "Are you sure you want to continue ? y (partition)/n (choose another drive) /x (continue) " ynx
case $ynx in
[Yy]* )
containsElement "$DISK"
if [ $? == 1 ]; then
initializeDisk $DISK
# remove element from found disk to prevent trying to partition it again.
delete=($DISK)
FOUNDDISKS=( "${FOUNDDISKS[#]/$delete}" )
else
echo "${red}$DISK is not a valid choice, please select a valid disk.${reset}"
chooseDisks
fi
break;;
[Nn]* )
chooseDisks
break $((INIT_STARTED));;
[Xx]* )
return
break;;
* ) echo "Please answer y or n. x to continue the script.";;
esac
done
# Any additional disks to partition?
while true; do
read -p "Do you have any additional drives to partition ? y/n " yn
case $yn in
[Yy]* )
#chooseDisks $FOUNDDISKS
chooseDisks
break $((INIT_STARTED));;
[Nn]* )
return
break $((INIT_STARTED));;
* ) echo "Please answer y or n";;
esac
done
}
I expect this:
break $((INIT_STARTED));;
to end the nth loop and exiting the function.
Don't play with nested logic break, just use some variable like $userStop and instead of while true; do put
userStop = false
while[!${userStop}]
do
#...
# replace break $((INIT_STARTED));; by
# userStop = true
I ended up changing the code to avoid breaking within a loop.
Thanks guys for directing me the right way.
David
i expect to break out of all nested loops and continue with the script
You can run the function in a subshell and use exit.
chooseDisks()
{
if [ "$1" -eq 0 ]; then
echo "The user entered it all!"
exit 0
fi
echo "The user is still entering... $1"
( chooseDisks $(($1 - 1)) )
}
# Imagine the user 5 times enters something
( chooseDisks 5 )
But the best would be to refactor your code to just have a big while true; do loop in the beginning. There is no need to make this function recursive.
There's this daemon with, for ex. 5 types in one script. Now, i want to be able to start/stop it by specifying the number of the daemon(to start one by one), OR specify "all" (to start in bulk).
The format: (runscript) (commandName) (daemon # or "all")
Need to satisfy two conditions, when the user inputs: (1) correctly (either by number or "all) OR
(2) incorrectly (either inputted num is greater than $count or all other string than "all").
All conditions are already achieved except for one, if the user inputs other string than "all"
Sample code:
case 'startDaemon': #commandName
set count = 5
if ($#argv == 2 && $2 == all) then
echo "correct, do this"
else if ($#argv == 2 && $2 < $count) then
echo "correct too, do this"
else if ($#argv == 2 && ($2 != all || $2 >= $count)) then
echo "Incorrect parameter: specify daemon # less than $count or 'all' to start all."
else
echo "Please use: $0(runscript) $1(commandname) (daemon # or all)"
whenever I type: (runscript) startDaemon hello, for example, error shows:
if: Expression syntax
When it should have gone to the 3rd condition. Please help and kindly point out if the prob is in the conditions or logical operators or whatever. Thanks in advance
PS. Im using csh. The script given to me is in csh, so yep.
The immediate problem is the comparison $2 < $count, which is invalid when $count contains a string.
Here is a working solution:
#!/bin/csh
set count = 5
if ($#argv == 2) then
if ($2 == all) then
echo "correct, do this"
else if (`echo $2 | grep '^[0-9]*$'`) then
if ($2 < $count) then
echo "correct too, do this"
else
echo "Number must be less than 5"
endif
else
echo "Incorrect parameter: specify daemon # less than $count or 'all' to start all."
endif
else
echo "Please use: $0(runscript) $1(commandname) (daemon # or all)"
endif