How to test whether wc -l is even or odd in shell?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
say I have a file called file1
$ a=$(wc -c file1)
$ echo $a
233 file1
now how would I do this
$ b=$(expr $a % 2)
$ echo $b
1
I guess the file1 in wc -c make's this crash?
shell
add a comment |Â
up vote
0
down vote
favorite
say I have a file called file1
$ a=$(wc -c file1)
$ echo $a
233 file1
now how would I do this
$ b=$(expr $a % 2)
$ echo $b
1
I guess the file1 in wc -c make's this crash?
shell
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
say I have a file called file1
$ a=$(wc -c file1)
$ echo $a
233 file1
now how would I do this
$ b=$(expr $a % 2)
$ echo $b
1
I guess the file1 in wc -c make's this crash?
shell
say I have a file called file1
$ a=$(wc -c file1)
$ echo $a
233 file1
now how would I do this
$ b=$(expr $a % 2)
$ echo $b
1
I guess the file1 in wc -c make's this crash?
shell
asked Feb 26 at 0:35
Tinler
1295
1295
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
Another way to remove the filename from wc
is to use wc -c <file1
. This avoids the requirement of a second process cat
.
add a comment |Â
up vote
2
down vote
I would use something like:
if [ $(( $(wc -c < your-filehere ) % 2)) -eq 1 ]
then
echo file has odd number of bytes
else
echo file has even number of bytes
fi
add a comment |Â
up vote
0
down vote
One way to go:
a=$(cat file1 | wc -c)
this doesn't output the file name and your expr
will work.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Another way to remove the filename from wc
is to use wc -c <file1
. This avoids the requirement of a second process cat
.
add a comment |Â
up vote
1
down vote
accepted
Another way to remove the filename from wc
is to use wc -c <file1
. This avoids the requirement of a second process cat
.
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Another way to remove the filename from wc
is to use wc -c <file1
. This avoids the requirement of a second process cat
.
Another way to remove the filename from wc
is to use wc -c <file1
. This avoids the requirement of a second process cat
.
answered Feb 26 at 0:41
dsstorefile1
1,576212
1,576212
add a comment |Â
add a comment |Â
up vote
2
down vote
I would use something like:
if [ $(( $(wc -c < your-filehere ) % 2)) -eq 1 ]
then
echo file has odd number of bytes
else
echo file has even number of bytes
fi
add a comment |Â
up vote
2
down vote
I would use something like:
if [ $(( $(wc -c < your-filehere ) % 2)) -eq 1 ]
then
echo file has odd number of bytes
else
echo file has even number of bytes
fi
add a comment |Â
up vote
2
down vote
up vote
2
down vote
I would use something like:
if [ $(( $(wc -c < your-filehere ) % 2)) -eq 1 ]
then
echo file has odd number of bytes
else
echo file has even number of bytes
fi
I would use something like:
if [ $(( $(wc -c < your-filehere ) % 2)) -eq 1 ]
then
echo file has odd number of bytes
else
echo file has even number of bytes
fi
answered Feb 26 at 1:15
Jeff Schaller
31.2k846105
31.2k846105
add a comment |Â
add a comment |Â
up vote
0
down vote
One way to go:
a=$(cat file1 | wc -c)
this doesn't output the file name and your expr
will work.
add a comment |Â
up vote
0
down vote
One way to go:
a=$(cat file1 | wc -c)
this doesn't output the file name and your expr
will work.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
One way to go:
a=$(cat file1 | wc -c)
this doesn't output the file name and your expr
will work.
One way to go:
a=$(cat file1 | wc -c)
this doesn't output the file name and your expr
will work.
answered Feb 26 at 0:39
man0v
30917
30917
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426571%2fhow-to-test-whether-wc-l-is-even-or-odd-in-shell%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password