0
511@ubuntu:~/Unix$ cat pass

hellounix
file1

#!/usr/bin
echo "Enter File Name"
read file
if [ -f $file ]
 then
    echo "File Found with a Name $file\n"
    echo "File Content as below\n"
    count=0
    val=0
    while read line
        do
        val=$("$line" | wc -c)
        echo "$line -->Containd $val Charecter"        
        count=$((count+1))
        done < $file
    echo "Total Line in File : $count"
else 
    echo "File Not Found check other file using running script"
fi

Output :

511@ubuntu:~/Unix$ sh digitmismatch.sh
Enter File Name
pass
File Found with a Name pass

File Content as below

digitmismatch.sh: 1: digitmismatch.sh: hellounix: **not found**
hellounix -->Containd 0 Charecter
digitmismatch.sh: 1: digitmismatch.sh: file1: **not found**
file1 -->Containd 0 Charecter
Total Line in File : 2
==============================================================

Why the value of wc -c is not assigned to the variable val?

1
  • Note the typing mistake on the first line of the script - /usr/bin is not a valid interpreter. Try #!/bin/bash Commented Aug 11, 2014 at 11:45

2 Answers 2

1

Your line is:

val=$("$line" | wc -c)

This tries to run the command given by $line and run the output through wc -c. The error message you see indicates that it's trying to run a "hellounix" command, as in the first line of your file. If you want to pass the value of the variable into the command instead, you can use printf:

val=$(printf '%s' "$line" | wc -c)

If you're using Bash, zsh, or another more powerful shell, you can also use here strings:

val=$(wc -c <<<"$line")

<<< performs expansion on the string "$line" and then provides it as the standard input of wc -c.


In this particular case, though, you can use the shell's built-in parameter expansion to get the length of the variable value without a pipeline at all:

val=${#line}

The # expansion expands to:

String Length. The length in characters of the value of parameter shall be substituted. If parameter is '*' or '@', the result of the expansion is unspecified. If parameter is unset and set -u is in effect, the expansion shall fail.

0
0
val=$("$line" | wc -c)

This line means "assign the output of the command "$line" | wc -c to the variable val". Let's say $line is holding hellounix, so it now looks like this:

val=$(hellounix | wc-c)

It's trying to run a command called hellounix, which is not found, that's why you're getting the hellounix: not found error.

A simple fix would be to add an echo, like this:

val=$(echo -n "$line" | wc -c)

This will "echo" the line to stdout, and then passes it on to wc. The -n option removes the newline character at the end of the line, because wc counts it. If you want to count the newline character at the end of the line, remove the -n option.

With echo -n:

Enter File Name
testfile
File Found with a Name testfile

File Content as below

hellounix -->Containd 9 Charecter
file1 -->Containd 5 Charecter
Total Line in File : 2

With only echo:

Enter File Name
testfile
File Found with a Name testfile

File Content as below

hellounix -->Containd 10 Charecter
file1 -->Containd 6 Charecter
Total Line in File : 2
1
  • Concepts also has been clear.. Thanks a TON.. :) Commented Aug 12, 2014 at 4:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.