Execution problem with shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execution problem with shell script
# 1  
Old 10-15-2010
Execution problem with shell script

Hi all,
I want to use perl string manipulation commands in my shell script.
I have written following script.

Code:
 
echo "enter name"
read name
perl -e '$m=length($name);
echo $m

it gives an error: unrecognized token in perl command line.
do not suggest me an equivalent command of shell script.
just tell me what should i do to embed perl command in shell script?
thanks

Last edited by vbe; 10-15-2010 at 10:01 AM.. Reason: Code tags please
# 2  
Old 10-15-2010
Unnecessary to use an external program, use the shell built-in:
Code:
echo ${#name}

# 3  
Old 10-15-2010
More generally, count any number of bytes from any source with "wc -c" (but for this it would count the line feed, too, so I echo without linefeed):

echo "$name\c" | wc -c | read name_len
# 4  
Old 10-16-2010
Quote:
Originally Posted by DGPickett
More generally, count any number of bytes from any source with "wc -c" (but for this it would count the line feed, too, so I echo without linefeed):

echo "$name\c" | wc -c | read name_len

That will not work for most people:
Code:
$ name=chris
$ echo "$name" | wc -c
6
$ echo "$name\c" | wc -c
8

Also, for most people, name_len will not contain the result you want. In most shells, all elements of a pipeline are executed in a subshell.
Use command substitution instead:
Code:
name_len=$( printf "%s" "$name" | wc -c )

Or better, as others have already pointed out, use the shell's own length expansion:
Code:
echo "${#name}"

This User Gave Thanks to cfajohnson For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execution problem with shell script for modifying a user

#/bin/sh echo "enter the user name" read $username echo "Enter new home directory" read $newhd usermod -d $newhd $username ;; error while executing : enter the user name Rev Enter new home directory: /home/58745 usermod: option requires an argument -- 'd' Try `usermod --help' or... (2 Replies)
Discussion started by: Revanth547
2 Replies

2. Solaris

Script on Solaris spawning 2 processes for one shell script execution

Hi, I am having a shell script on Solaris 10 which has a while loop as shown below. #!/usr/bin/ksh # while do sleep 60 done Name of the shell script is coldcentric.sh. I executed script /DATAWAREHOUSE/LOAD/Scripts/coldcentric.sh from a command task in Informatica worklow as... (3 Replies)
Discussion started by: chekusi
3 Replies

3. Solaris

Execution problem in shell script while insert into DB

Hi, am facing some problem while inserting a record into a script Please find script below. `sqlplus -s asdf/asdf123 <<eof! set feedback off; set heading off; set verify off; insert into... (2 Replies)
Discussion started by: senkerth
2 Replies

4. IP Networking

Problem with script execution from a DHCP event

Hi, I'm installing a DHCPD Server with the packages from a distro SLES11 SP1 (dhcp-server-3.1.1-7.12). And a DNS Server with PowerDNS: pdns-recursor-3.3-1 pdns-static-3.0-1 The DHCP update de DNS Server, but PowerDNS is not RFC 2135 compliant, and I have to update the MySQL register... (5 Replies)
Discussion started by: bypper
5 Replies

5. Shell Programming and Scripting

Execution problem with csh script

Hi All, I have a small issue with my csh script which I am using to FTP a file. What I know is...there are two commands to execute script.. 'sh <file>' & '\<file>'. When I execute my script with command 'sh <file>', it gives me syntax error while it runs successfully with command '\<file>'. I am... (3 Replies)
Discussion started by: ndd
3 Replies

6. Shell Programming and Scripting

Execution problem with grep script (2 variables)

#!\bin\sh TEST=test.log GREP=\usr\bin\grep $GREP -i 'dog\|cat' ${TEST} Why doesn't grep run at all? (10 Replies)
Discussion started by: jazzaddict
10 Replies

7. Shell Programming and Scripting

parallel execution of script/ synchro problem

Hi everybody, In a csh script, i need to run 4 time the same prog with different parameters. What i want is to run them in parallel. for this i use the command toto1.sh & toto2.sh & toto3.sh & toto4.sh For this I have no problem. In fact, I need to wait until all the programs are over to... (2 Replies)
Discussion started by: Moumou
2 Replies

8. Shell Programming and Scripting

problem with remote execution of script using telnet

Hi all, i am trying to remotely execute a script from a different server. this is the code that i use : #!bin/sh pwd (sleep 1 echo "username" sleep 2 echo "pwd" sleep 2 echo "cd /path/to/file" if then echo "script1.sh" echo "mailx -s "Task Executed"... (1 Reply)
Discussion started by: sais
1 Replies

9. Shell Programming and Scripting

problem with shell script execution

Hi All, i am running a shell script in which there is a command `ps -ef | grep smon > db` When i execute this command in the command prompt i am getting the desired output..but when the script is executed..the db file is getting created but with no values...I could not find the reason for... (2 Replies)
Discussion started by: anju
2 Replies

10. Shell Programming and Scripting

execution of shell script

How can I execute another shell script from one? Malay (5 Replies)
Discussion started by: malaymaru
5 Replies
Login or Register to Ask a Question