shell


Shell Error while trying to perform a IF statement


Good day.
I am trying to create a script to read each line of a specific file and then send each line to a second file depending on how many characters that line contains.
For example i have a file called numbers which contain the following information:
numbers.txt
5196803638
31995500317
5196396080
51999205240
5198158891
As you can see i have number with 8 or 9 numbers on each, i was trying to create a script that read a file line and send each number to its specific files.
!#/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
echo $line
if [ "${#line}"= 8 ]; then
$line> file8.txt
elif [ "${#line}"= 9 ]; then
$line> file9.txt
else
echo "Error"
fi
done < "$1"
But the only thing i get is the following
51996803638
My.sh: line 7: [: 11=: unary operator expected
My.sh: line 9: [: 11=: unary operator expected
Error
The shell's tokenizer is white-space sensitive. Use spaces around =, as in
if [ "${#line}" = 8 ]; then
$line> file8.txt
elif [ "${#line}" = 9 ]; then
$line> file9.txt
else
echo "Error"
fi
Or, what about this, which I find more readable/maintainable/extendable:
case ${#line} in
(8) $line > file8.txt;;
(9) $line > file9.txt;;
(*) echo Error;;
esac
Your numbers have 10 or 11 characters. When you compare numerical values, use -eq, -ne, -lt, -gt, -le, -ge.
while IFS='' read -r line || [[ -n "$line" ]]; do
echo $line
echo "Length = ${#line}"
if [ ${#line} -eq 10 ]; then
echo $line >> file10.txt
elif [ ${#line} -eq 11 ]; then
echo $line >> file11.txt
else
echo "Error"
fi
done < "$1"

Related Links

Answer multiple questions from script shell
Memory Requirements of Shell Variable Assignment
Unable to create directory in jenkins using shell command
rm -rf /base-dir-path/*/work isn't the same as /base-dir-path/*/*/work
Aid with tcsh shell script for protein research
Strange behaviour of declare/typeset with “options” array in zsh
Create a directory at user's home directory throught a shell script with command mkdir ~/adirectory
How to process values from for loop in shell script
Shell Scripting including code from an other file
Script shell echo result of command has unknown values
Parse and rename date in filename
Compare integral numbers in a makefile?
Please tell me what dose 1) -n option 2) sed 's?.*/??'
Finding files by type and renaming them based on their parent directory
Cron - gsutil not found
Batch script - calling commands mid-script

Categories

HOME
beautifulsoup
kentico
gridview
design-patterns
rdbms
interface
mstest
apiblueprint
spring-data-redis
simulink
big-o
interpolation
x264
jquery-ui
computer-science
windows-ce
redis-sentinel
x-cart
jxl
ipmitool
naivebayes
do-while
selinux
babel
libigl
pimcore
event-store
jboss6.x
moses
maze
jasmine-node
user-defined-functions
boost-asio
scrollview
deltaspike
git-squash
mailkit
imx6
servicestack-text
tex
z-notation
cin
google-data-api
branching-and-merging
postgresql-8.4
proget
hammer.js
zenhub
singleinstance
page-replacement
backbone-collections
oozie-coordinator
seq
java-websocket
pytables
centrifuge
adafruit
cda
false-sharing
crouton-os
themoviedb-api
dllimport
microsoft-reporting
coda
jsvc
typeconverter
nsurlsessiondatatask
fail2ban
nodelist
structuremap3
contrast
genymotion-call
raygun
reflector
multitasking
windows-embedded-standard
eclipse-kepler
embedded-fonts
model-driven-development
flex++
node-blade
insert-id
cascadingdropdown
zope.interface
pyunit
subproject
josql
mapping-by-code
stress
node-redis
user-tracking
workflow-services
promotion-code
nhprof
object-database
interprocess
scrollbars
duplex
linfu-dynamicproxy
firefox-3
image-capture

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App