This is the prompt for my script:
Script that asks the user “Are you OK?”
If user replies y or Y, then say “glad to hear it” else if the user enters n or N then print “sorry that you are not feeling good”. If the user enters some other character, then print in-correct choice and ask the question again.
Here's what I have:
#! /bin/csh
echo "Are you OK? "
set n = $<
set loop = 1
if (("$n" == "y") || ("$n" == "Y")) then
echo "glad to hear it"
else if (("$n" == "n") || ("$n" == "N")) then
echo "sorry that you are not feeling good"
else
echo "in-correct choice"
while ( $loop == 1 )
echo "Are you OK? "
set n = $<
if (("$n" == "y") || ("$n" == "Y")) then
echo "glad to hear it"
set loop = 0
else if (("$n" == "n") || ("$n" == "N")) then
echo "sorry that you are not feeling good"
set loop = 0
else
echo "in-correct choice"
endif
end
endif
I keep receiving the error "else: endif not found." Also the echo line "glad to hear it" runs every time regardless if the user input is right or not. Please help. Thank you
Just add new line after Your last endif statement:
...
end
endif
#new line
Or I recommend always end csh scripts with exit status and You should be good to go:
...
end
endif
exit (0)
Anyway here is also little rewrite of Your script:
#!/bin/env csh
while (1)
echo "Are you OK?"
set n = "$<"
if (("$n" == "y") || ("$n" == "Y")) then
echo "glad to hear it"
break
else if (("$n" == "n") || ("$n" == "N")) then
echo "sorry that you are not feeling good"
break
else
echo "in-correct choice"
endif
end
exit (0)
set n = $< here is danger better set n = "$<" in order not to handle e.g. string y abc as yes
Related
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
I have trouble with this code. Are my assigns properly done? I gave "q: Command not found.Badly placed ()'s."
#!/bin/tcsh
set h = 0
set q = 0
set a = 0
foreach val ( $* )
if ($val == "-h")then
h = 1
endif
if ($val == "-q")then
q = 1
endif
if ($val != "-h") && ($val != "-q")then
a = 1
endif
end
Building on the comments about the ()s and spaces, here is a version that works:
#!/bin/tcsh
set h = 0
set q = 0
set a = 0
foreach val ( $* )
if ($val == "-h") then
set h = 1
endif
if ($val == "-q") then
set q = 1
endif
if ($val != "-h" && $val != "-q") then
set a = 1
endif
end
Added spaces after )s
Put && within the ()s
Use set to set variables. Bourne shell just has x=y but *csh requires set x=y.
You might also want to look into getopt -- if you have a Linux or GNU version of that command available, it can make option parsing a lot more standardized.
I am coding in bash ,using Ubuntu 18.04, and I am playing around with kdialog. I made a simple magic eight ball themed program and I am unable to close the input box and exit the program, instead I get stuck in a loop. This code was originally made in BASH dialog and I decided to change it to kdialog. Any help would be greatly appreciated. It is something simple that I am overlooking.
#!/bin/bash
#version 3
OUTPUT="TEMP.txt"
>$OUTPUT
while [ true ]
do
shuffle() {
local i tmp size max rand
size=${#array[*]}
max=$(( 32768 / size * size ))
for ((i=size-1; i>0; i--));
do
while (( (rand=$RANDOM) >= max ));
do :;
done
rand=$(( rand % (i+1) ))
tmp=${array[i]}
array[i]=${array[rand]}
array[rand]=$tmp
done
}
array=( 'It Is Certain'
'Without A Doubt'
'Maybe'
'Signs Point To Yes'
'Most Likely'
'As I See It, Yes'
'Ask Again Later'
'Concentrate And Ask Again'
'HAHAH No..'
'Ask Again'
'Have Faith In Yourself'
'Very Doubtful'
'Outlook Not So Good'
'My Sources Say No'
'Unknown At This Time'
'Could Happen Any Moment Now'
'Is That A Joke?'
'Unlikely' )
shuffle
function sayhello(){
local n=${array[#]}-""
#display it
kdialog --msgbox "This Is What I See: ${array}"
#--clear --msgbox "${array}" 8 41
}
# show an inputbox
kdialog --title "Welcome " \
--inputbox "Ask and you shall recieve great fortune: " #8 60
function think_tank(){
progress=$(kdialog --progressbar "hmmm Let Me Think..." 4);
sleep 1;
qdbus $progress Set "" value 1 > /dev/null;
sleep 1;
qdbus $progress Set "" value 2 > /dev/null;
sleep 1;
qdbus $progress Set "" value 3 > /dev/null;
sleep 1;
qdbus $progress Set "" value 4 > /dev/null;
sleep 1;
qdbus $progress close > /dev/null;
sleep 1
#kdialog --title "This is a passive popup" --passivepopup \
#"It will disappear in about 10 seconds" 10
}
# get response
response=$?
# get data stored in $OUPUT using input redirection
name=$(<$OUTPUT)
case $response in
0)
think_tank
sayhello ${array[#]}
;;
1)
echo "Goodbye For Now."
exit 0
;;
255)
echo "Goodbye For Now."
exit 0
;;
esac
#rm $OUTPUT
done
done
After some sleep I easily figured this issue out. I removed the case statement and used if statements instead. The program would not break out of the case statement due to a return 0 from kdialog's --msgbox.
#made some quick msgbox functions
if [ "$?" = 0 ];
then
think_tank #progress bar
msg_box #results
elif [ "$?" = 1 ];
then
goodbye #closing message box
exit 0;
else
error #error message box
exit 0;
fi;
I am new to bash scripting and I am practicing some code.
I am trying to create a script that displays the following output and loops until the user types x or X
Menu 1
C) Calculation
X) Exit
C
Menu 2
Enter an integer or press X to exit:
22
Menu 3
+) Add
-) Subtract
+
Menu 2
Enter an integer or press X to exit:
33
The sum of 22 and 33 is 55
Menu 1
C) Calculation
X) Exit
c
Menu 2
Enter an integer or press X to exit:
50
Menu 3
+) Add
-) Subtract
-
Menu 2
Enter an integer or press X to exit:
23
The difference of 50 and 23 is 27
Menu 1
C) Calculation
X) Exit
X
Here is my code for it:
add ()
{
((sum=n1 + n2))
echo "The sum of $n1 and $n2 is $sum"
exit
}
subtract ()
{
((difference=n1 - n2))
echo "The difference of $n1 and $n2 is $difference"
exit
}
while true
do
echo "Menu 1"
echo "C) Calculation"
echo "X) Exit"
read opr
if [ ${opr} = 'x' ] || [ ${opr} = 'X' ]
then
break
elif [ ${opr} = 'c' ] || [ ${opr} = 'C' ]
then
echo "Menu 2"
echo "Enter an integer or press X to exit:"
fi
read n1
if [ $n1 = 'x' ] || [ $n1 = 'X' ]
then
break
else
echo "Menu3"
echo "+) Add"
echo "-) Subtract"
fi
read opr
if [ $opr = '+' ]
then
echo "Please enter another integer to perform addition"
read n2
add
elif [ $opr = '-' ]
echo "Please enter another integer to perform subtraction"
read n2
subtract
fi
done
I am receiving this error message:
./myscript.sh: line 72: syntax error near unexpected token fi'
./myscript.sh: line 72:fi'
I believe if I make menu1, menu2, and menu3 into functions I could achieve what I desire my output to be instead of this version of it.
But I know that I will still have a problem with those fi ... any idea where I should put them or what do I need to do for my code to work and not give an error?
Thanks
Pill
You simply forgot an then after elif
Look the general example from http://www.thegeekstuff.com/2010/06/bash-if-statement-examples/
If [ conditional expression1 ]
then
statement1
statement2
.
elif [ conditional expression2 ]
then
statement3
statement4
.
.
.
else
statement5
fi
In your code:
...
elif [ $opr = '-' ]
then <<<<<<< missing in your code!
echo "Please enter another integer to perform subtraction"
read n2
subtract
fi
EDIT: Why the program did not loop
If you write exit in your add and subtract method, your script will exit. Why it should not? Tip: remove both exit and the loop have a chance :-)
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