LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-26-2008, 03:02 AM   #1
kamlesh_kmr
LQ Newbie
 
Registered: May 2008
Posts: 21

Rep: Reputation: 0
Angry 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
 
Old 11-26-2008, 03:29 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 11-27-2008, 06:25 AM   #3
kamlesh_kmr
LQ Newbie
 
Registered: May 2008
Posts: 21

Original Poster
Rep: Reputation: 0
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
 
Old 11-27-2008, 07:51 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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>".

Last edited by colucix; 11-27-2008 at 08:27 AM.
 
Old 12-01-2008, 04:16 AM   #5
kamlesh_kmr
LQ Newbie
 
Registered: May 2008
Posts: 21

Original Poster
Rep: Reputation: 0
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
 
Old 12-01-2008, 05:08 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 12-01-2008, 08:16 AM   #7
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Erroneous post sorry!

Last edited by dive; 12-01-2008 at 08:20 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
TOP command: calculation of memory usage samiralmousawi Linux - Server 7 07-29-2008 03:16 PM
Unable to Perform Orderly Shutdown JonBL Linux - Software 5 07-02-2007 06:33 AM
Unable to perform zone transfer for DNS feef Linux - Networking 3 04-24-2006 03:10 PM
k3b unable to PERFORM OPC: Input/output error librano Linux - Software 2 04-18-2006 10:18 AM
unable to perform apt-get update Skaan Debian 8 05-25-2005 09:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration