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 |
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 |
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 |
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 Code:
#!/bin/bash Code:
IFS The Internal Field Separator that is used for word splitting after expansion and to split |
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 |
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. |
Erroneous post sorry!
|
All times are GMT -5. The time now is 12:43 AM. |