Le script KornShell (ksh) suivant doit vérifier si la chaîne est un palindrome. J’utilise ksh88
, pas ksh93
.
#!/bin/ksh strtochk="naman" ispalindrome="true" len=${#strtochk} i=0 j=$((${#strtochk} - 1)) halflen=$len/2 print $halflen while ((i < $halflen)) do if [[ ${strtochk:i:1} == ${strtochk:j:1} ]];then (i++) (j--) else ispalindrome="false" break fi done print ispalindrome
Mais if [[ ${strtochk:i:1} == ${strtochk:j:1} ]];then
erreur de substitution incorrecte à la ligne suivante: if [[ ${strtochk:i:1} == ${strtochk:j:1} ]];then
Quelqu’un peut-il s’il vous plaît laissez-moi savoir ce que je fais mal?
La syntaxe de la sous-chaîne dans ${strtochk:i:1}
et ${strtochk:j:1}
n’est pas disponible dans ksh88. Mettez à niveau vers ksh93 ou utilisez un autre langage comme awk ou bash.
Vous pouvez remplacer votre test par cette ligne portable:
if [ "$(printf "%s" "$strtochk" | cut -c $i)" = "$(printf "%s" "$strtochk" | cut -c $j)" ]; then
Vous devez également remplacer le douteux
halflen=$len/2
avec
halflen=$((len/2))
et la syntaxe ksh93 / bash:
$((i++)) $((j--))
avec celui de ksh88:
i=$((i+1)) j=$((j-1))
Que diriez-vous de ce script KornShell (ksh) pour vérifier si une chaîne d’entrée est un palindrome.
isPalindrome.ksh
#!/bin/ksh #----------- #---Main---- #----------- echo "Starting: ${PWD}/${0} with Input Parameters: {1: ${1} {2: ${2} {3: ${3}" echo Enter the ssortingng read s echo $s > temp rvs="$(rev temp)" if [ $s = $rvs ]; then echo "$s is a palindrome" else echo "$s is not a palindrome" fi echo "Exiting: ${PWD}/${0}"