This question already has answers here:
Cannot debug simple ksh programme
(2 answers)
Closed 8 years ago.
The following KornShell (ksh) script should check if the string is a palindrome. I am using ksh88, not 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
But I am getting bad substitution error at the following line : if [[ ${strtochk:i:1} == ${strtochk:j:1} ]];then
Can someone please let me know what I am doing wrong?
The substring syntax in ${strtochk:i:1} and ${strtochk:j:1} is not available in ksh88. Either upgrade to ksh93, or use another language like awk or bash.
You can replace your test with this portable line:
if [ "$(printf "%s" "$strtochk" | cut -c $i)" =
"$(printf "%s" "$strtochk" | cut -c $j)" ]; then
You also need to replace the dubious
halflen=$len/2
with
halflen=$((len/2))
and the ksh93/bash syntax:
$((i++))
$((j--))
with this ksh88 one:
i=$((i+1))
j=$((j-1))
How about this KornShell (ksh) script for checking if an input string is a palindrome.
isPalindrome.ksh
#!/bin/ksh
#-----------
#---Main----
#-----------
echo "Starting: ${PWD}/${0} with Input Parameters: {1: ${1} {2: ${2} {3: ${3}"
echo Enter the string
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}"
Related
This question already has answers here:
Ternary operator (?:) in Bash
(23 answers)
Bash command groups: Why do curly braces require a semicolon?
(1 answer)
Closed 2 years ago.
Can a conditional statement be inserted in, for example, a print command like echo with bash?
E.g. (does not work)
$ cat t.sh
#!/bin/bash
string1="It's a "
string2opt1="beautiful"
string2opt2="gross"
string3=" day."
read c
echo -n "$string1 $( [[ $c -eq 0 ]] && { echo -n "$string2opt1" } || { echo "$string2opt2" } ) $string3"
I know that this is possible with semicolons/non-one-liners; I am just wondering if there is a more elegant or accepted way of doing this.
To clarify, you want to achieve this:
#!/bin/bash
string1="It's a"
string2opt1="beautiful"
string2opt2="gross"
string3="day."
read -r c
if [[ $c -eq 0 ]]; then
echo "$string1" "$string2opt1" "$string3"
else
echo "$string1" "$string2opt2" "$string3"
fi
but in a one-liner. Does this work for you?
#!/bin/bash
string1="It's a"
string2opt1="beautiful"
string2opt2="gross"
string3="day."
read -r c
echo "$string1" "$([[ $c -eq 0 ]] && echo "$string2opt1" || echo "$string2opt2")" "$string3"
My problem is pretty simple. I have :
a=$(echo "lol")
for i in {1..3};
do
echo $a && echo $i ;
done
I get :
lol
1
lol
2
lol
3
I would like to print only once the variable a at the beginning of the output , to get :
lol
1
2
3
Any idea?
You don't need a loop at all
a=$(echo "lol") # Not sure why poster wrote this rater than a=lol
printf %s\\n "$a" {1..3}
I suggest:
#!/bin/bash
a="lol"$'\n' # append newline
for i in {1..3}
do
echo -e "$a$i" # -e: enable interpretation of escape sequences
unset a
done
Or replace in your question
echo $a && echo $i ;
with
[[ "$i" == "1" ]] && echo "$a"
echo "$i"
See: help echo and help unset
Move the echo outside of the for loop
a=$(echo "lol")
echo $a
for i in {1..3}; do
echo $i;
done
or:
echo "lol"
for i in {1..3}; do
echo $i;
done
test run in shell
I am trying to read a parameter file in a shell script and would want to skip the lines which start with "#". Have been trying it on Ubuntu VM (default bash) and for something that I can't understand, it doesn't seem to work.
Following is the pseudo-code that I am using:
while read line
do
if [ grep -q "#" <<< "$line" ]; then
## Do nothing (Commented Out)
echo "$line Line is Commented out"
elif [ "$line" = "" ]; then
## Do nothing (Blank Line)
echo "Blank line"
else
#echo "read line is $line"
...some logic here
fi
done <input_file.ini
This yields the the following error: Syntax error: redirection unexpected
The if [[ $line == *#* ]] construct doesn't seem to work. My earlier experience was on AIX where everything worked fine.
Could someone guide me what I am doing wrong here?
PS: On a related note, how do I handle cases where I don't want to do anything? e.g. when there is no '#' character in the read line, I don't want to do anything. I can't leave my if block blank so I am just using echo 'some random' text. My task works good but just wanted to understand what's a good practice to handle this.
Your code is clearly running with /bin/sh, not bash.
An alternative to [[ $line = *"#"* ]] that works with /bin/sh is case.
Thus, the following will work with /bin/sh, or when invoked with sh yourscript:
#!/bin/sh
while read -r line; do : line="$line"
case $line in
*"#"*) echo "Line is commented out: $line";;
"") echo "Line is empty" ;;
*) key=${line%%=*}
value=${line#*=}
eval "$key="'$line' # unsafe, but works with /bin/sh, which doesn't have better
# indirect assignment approaches.
printf '%s\t\t-\t\t%s\n' "$key" "$value"
;;
esac
done <input_file.ini
Alternately, consider putting in a guard to handle the case when your script is invoked with a non-bash shell:
#!/bin/bash
case $BASH_VERSION in
'')
echo "ERROR: Run with a non-bash shell" >&2
if [ "$tried_reexec" ]; then
echo "ERROR: Already attempted reexec and failed" >&2
exit 1
fi
if [ -s "$0" ]; then
export tried_reexec=1
exec bash "$0" "$#"
fi
;;
esac
while read -r line; do
if [[ $line = *"#"* ]]; then
echo "Line is Commented out: $line"
elif [[ "$line" = "" ]]; then
echo "Blank line"
else
key=${line%%=*}; value=${line#*=}
printf -v "$key" %s "$value"
printf '%s\t\t-\t\t%s\n' "$key" "$value"
fi
done <input_file.ini
I really wasn't able to figure out the exact problem with double [[ ]] and the character search in a string. Thanks to everyone who tried to help me out. However this was acting as a deterrent and I didn't want to continue to fiddle for too long, I used a slightly different approach to handle my situation.
The following code works for me now:
while read line
do
first_char=`echo $line | cut -c 1`
if [ "$first_char" = "#" ]; then
: "do nothing here. Line is commented out"
elif [ "$line" = "" ]; then
: "do nothing here. Blank line"
else
KEY="$(echo $line | cut -d '=' -f1)"
VALUE="$(echo $line | cut -d '=' -f2)"
printf \v "$KEY" %s "$VALUE"
echo "$KEY\t\t-\t\t$VALUE"
fi
done < ${SCHEDULER_LOC}/inputs/script_params.ini
Also I was able to learn few things so incorporated them as well. I did get few negative scores for this question. Understandably so since this might be rudimentary for the experts but it was a genuine problem I was seeking some guidance on. Still, I am thankful that I learnt something new. Kudos to the community.
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
}
This question already has answers here:
How do I use floating-point arithmetic in bash?
(23 answers)
Closed 4 years ago.
How can I have the right result from this bash script?
#!/bin/bash
echo $(( 1/2 ))
I get 0 as result! So I tried to use these but without success:
$ echo $(( 1/2.0 ))
bash: 1/2.0 : syntax error: invalid arithmetic operator (error token is ".0 ")
$ echo $(( 1.0/2 ))
bash: 1.0/2 : syntax error: invalid arithmetic operator (error token is ".0/2 ")
bash is not the right tool alone to use floats, you should use bc with it :
bc <<< "scale=2; 1/2"
.50
If you need to store the result in a variable :
res=$(bc <<< "scale=2; 1/2")
echo $res
I once stumbled on a nice piece of code, which is somewhat utilizing suggestion sputnick made, but wraps it around a bash function:
function float_eval()
{
local stat=0
local result=0.0
if [[ $# -gt 0 ]]; then
result=$(echo "scale=$float_scale; $*" | bc -q 2>/dev/null)
stat=$?
if [[ $stat -eq 0 && -z "$result" ]]; then stat=1; fi
fi
echo $result
return $stat
}
Then, you can use it as:
c=$(float_eval "$a / $b")