matchge
asked on
how to read special characters in shell script
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
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
ASKER
Thank you very much.
But what if I want to input without the single quote?
Thanks for ur comments.
But what if I want to input without the single quote?
Thanks for ur comments.
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\$#
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\$#
ASKER
So,you mean I need to consider all the possibility $,#,\,'," etc? Is there any fast and convince way to handle them all?
Thank you ~~~
Thank you ~~~
ASKER
where can I find out how to handle other special characters? like #,space,',",\ ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
script 'file1$#' 'file2$%'