bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fwd: Strange results


From: Kerin Millar
Subject: Re: Fwd: Strange results
Date: Fri, 27 Oct 2023 16:59:46 +0100

On Fri, 27 Oct 2023 19:28:15 +0700
Victor Pasko <victor.pasko@gmail.com> wrote:

> Let me ask more questions presented in the sent bug2.bash
> Look at the following lines:
> 
> *printf -v a_int '%d' "'a" *# very strange syntax here to use char as
> integer
> echo "echo4 $a_int"       # should be 97 at the end

All implementations of the standard printf utility are required to support 
this, as expressed by the third-from-last paragraph of its EXTENDED 
DESCRIPTION, and the bullet points that follow it.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html

>
> 
> echo "echo5 *${ASCII_SET:$((a_int-12)):1}" *# you can see letter u at the
> end as at 97-12=85 place
> echo "echo5.1 ${ASCII_SET:a_int-12:1}"    # you can see letter u at the end
> as at 97-12=85 place
> 
> 1) Could you please suggest elegant way to cast string symbol to
> integer instead of printf for  * "'a"*

Your request implies that printf is ill-suited to the task but I do not see 
why. It allows for you to convert a character to its ordinal value according to 
the system's character type (LC_CTYPE). What is your objection to using it? If 
it simply that you don't consider it to be elegant, there are many other 
languages to choose from.

> 2) Here are some unsuccessful examples to use bitwise operations with
> symbols:
> 
> % echo $(("'a" >> 4))
> -bash: 'a >> 4: syntax error: operand expected (error token is "'a >> 4")

You are in an arithmetic context there, and must abide by its rules of syntax 
(which are mostly those of ANSI C, not the printf utility). Obtain the integer 
value first.

$ char=a; printf -v ord %d "'${char}"; echo $(( ord >> 4 ))
6

-- 
Kerin Millar



reply via email to

[Prev in Thread] Current Thread [Next in Thread]