Solved

how to read special characters in shell script

Posted on 2007-11-26
8
4,279 Views
Last Modified: 2013-12-26
How to read special characters in bash shell?
I need to read 2 file names which could contain special characters, like $ # etc.
I am appreciate someone can help me with this. this code is really make me crazy. Thanks
#!/bin/bash
#Process one file##############
read() {
     tempPoint=0
     line=1
     while [ $line -le $lineCount ]
     do    
	subPoint=`sed -n "$line"p $file | awk '{print $2}'`
        name=`sed -n "$line"p $file | awk '{print $1}'	`
        if [ -n "$name" ]
        then {
        if [ $Name = $name ]
        then
        {
        tempPoint=$subPoint
        }
        fi
        }
        fi
        line=`expr $line + 1`
    done
    if [ $tempPoint -ne 0 ]
    then
    	totalPoint=`expr $totalPoint + $tempPoint`
    fi
}
######main program###############
 
if [ $# -eq 0 ]
then
    echo "no arguments"
    echo
    exit 1
fi
if [ $# -eq 1 ]
then
    echo "no files"
    echo
    exit 1
fi
if [ $# -eq 2 ]
then
    echo "no files"
    echo
    exit 1
fi
Name=$1
shift
totalPoint=0
total=0
until [ $# -eq 0 ]
do
    if [ -f "$1" ]
    then
     {
     file=$1
     total=`expr $total + 1`
     lineCount=`wc -l < $file`
     read
    }
    fi
    shift
done
 
if [ $total -eq 0 ]
then
{
   echo "no files"
   echo
}
else
{
   echo $Name "AVG:" `expr $totalPoint / $total`
}
fi

Open in new window

0
Comment
Question by:matchge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
8 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 20355385
What problem are you having?  Works fine for me.  Remember that you should quote filenames with funny characters, eg:

script 'file1$#' 'file2$%'
0
 

Author Comment

by:matchge
ID: 20355465
Thank you very much.
But what if I want to input without the single quote?
Thanks for ur comments.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20355514
Very much depends on your shell.

For example in bash, you can set the GLOBIGNORE environment variable to ignore various glob characters such as * and ?, eg:

export GLOBIGNORE=*:?

However, for $, you need to escape it with either single quotes or backslash, eg:

script file1\$#
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:matchge
ID: 20355600
So,you mean I need to consider all the possibility $,#,\,'," etc? Is there any fast and convince way to handle them all?
Thank you ~~~
0
 

Author Comment

by:matchge
ID: 20355642
where can I find out how to handle other special characters? like #,space,',",\ ?
0
 
LVL 48

Accepted Solution

by:
Tintin earned 125 total points
ID: 20355824
Using single quotes is the most reliable, simple way of specifying meta-characters.  If the data also includes a single quote, you need to use a backslash, eg:

'abc\'def'
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 125 total points
ID: 20397628
> Is there any fast and convince way to handle them all?
simply prefix each character by \  (backslash)
Otherwise you have to use quotes or a shell which has no meta characters.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven disable workspace resolution 1 77
incorrect syntax near the order by 10 79
Programming Language for Wordpress 7 87
Where to download python requests library 6 105
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question