LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   unable to perform airthmetic calculation on value extracted by 'sed' command (https://www.linuxquestions.org/questions/programming-9/unable-to-perform-airthmetic-calculation-on-value-extracted-by-sed-command-686253/)

kamlesh_kmr 11-26-2008 03:02 AM

unable to perform airthmetic calculation on value extracted by 'sed' command
 
Hello All,

Please consider the following code which extracts the numeric part from given string.

var1="(version(543))"
var2=`echo $var1 | sed 's/(version(\([0-9]*\)))/\1/g'`
echo $var2

Output:
543

Now I want to perform airthmetic calculation on extracted part(543) using 'expr' command. When I use following command:

var3=`expr $var2 + 1`
echo $var3

then getting following error:

expr: non-numeric argument

Please let me know how can I perform airthmetic calculation.

thanks,
kamlesh

colucix 11-26-2008 03:29 AM

The syntax is correct. Are you sure you used the right value for var2? If using bash, try the following arithmetic operator:
Code:

# var2=543
# var3=$(($var2 + 1))
# echo $var3
544


kamlesh_kmr 11-27-2008 06:25 AM

thanks colucix, it's working.

one more query related to sed,

I am reading the keyboad input and want to replace this input with some text in file. The problem is that sed is removing space at the end of value if any. For example: If I enter 'abcd ' from keyboard, sed writes only 'abcd', it removes the space while I want to write the keyboard input as it is as.

here is my code:

read val
`sed -i 's/some_text/'$val'/' file_name

Keyboard input:
'abcd '

Output in the file;

'abcd'

Desired output:
'abcd '

Please let me know the correct syntex.

thanks,
kamlesh

colucix 11-27-2008 07:51 AM

You have to change the IFS (internal field separator) for the execution of the read statement, by setting it to a null string. Also embed $val in double quotes inside the sed line, otherwise sed complains about an unterminated 's' command due to the presence of the trailing blank space:
Code:

IFS= read val
sed -i 's/ALL/'"$val"'/' file_name

The problem is the read statement, not the insertion of $val inside the sed command. The trailing blank space is considered as field separator and therefore discarded from the value of the val variable. To demonstrate just run the following script and type "abcd " with the trailing space when prompted:
Code:

#!/bin/bash
read -p "Enter a string: " val
echo "The length of \$val is ${#val}"
echo
IFS= read -p "Enter a string: " val
echo "The length of \$val is ${#val}"
echo

An excerpt from the bash man page about IFS:
Code:

IFS  The Internal Field Separator that is used for word splitting after expansion and to split
    lines into words with the read builtin command. The default value is "<space><tab><new-line>".


kamlesh_kmr 12-01-2008 04:16 AM

colucix, thanks for the help. It works now.

I have one more query regarding shell script.

I have created one shell script myscript.sh with root user. Now I want to allow everyone to execute this script but don't want to allow anybody to either read(nobody should be able to read the contents of this script) or write permission on this script. How can I achieve this scenario?

thanks,
kamlesh

colucix 12-01-2008 05:08 AM

Well, there was a long debate about the actual necessity of hiding source code to the users, here at LQ. I don't reprise it now, but look for script encryption or something similar in the LQ search engine if you are interested in the subject.

The only way I tested (just out of curiosity) to hide script content is converting the script into an executable using the shc utility. You can keep your code safe under root's home and make the executable available to the users, e.g. under /usr/local/bin. However, every time you modify the source code, you have to create the executable again.

More in detail, you have to launch the shc utility with the proper options (see the man page). This will create the C source code and the executable itself. Just move the executable to /usr/local/bin (just an example) and eventually rename it and set the permission to make it executable by the users.

Anyway, I tested shc on very simple scripts. You have to test it deeply before make the resulting executable available to the users.

dive 12-01-2008 08:16 AM

Erroneous post sorry!


All times are GMT -5. The time now is 12:43 AM.