• We did not send an email asking for donations - please read this post.

Spacing in linux bash script

justinpajela

New Member
Joined
Nov 10, 2022
Messages
1
Reaction score
0
Credits
19
I have an issue whenever I run this script. It gets the right wordcount of the file but whenever I run it in the terminal it has unwanted spacing
#!/bin/bash
char=$(cat $1 | wc -c)
echo "This file has $char characters in it."
nolines=$(cat $1 | tr -d "\n" | wc -c)
echo "This file has $nolines characters not counting the new line."
emptyline=$(grep -cv '\S' $1) echo "This file has $emptyline empty lines."
alphachar=$(tr -cd '[:alpha:]' < $1 | wc -c)
echo "This file has $alphachar alphanumeric characters."

Using a file with this in it called example_file (this is the file under this, or the content of the file)
This is the first line
This is the second
This has the symbols @#$

There was just an empty line
So whenever I run my script like ~/script.sh example_file it gives an output of. n is space because it get rids of the space
This file has nnnnnn 93 characters in it.
This file has nnnnnn 88 characters not counting the new line.
This file has 1 empty lines.
This file has nnnnnn 70 alphanumeric characters.
 
Last edited:


wizardfromoz

Administrator
Staff member
Gold Supporter
Joined
Apr 30, 2017
Messages
7,783
Reaction score
6,608
Credits
28,627
@justinpajela welcome to linux.org.

I am moving this Thread to Command Line, where scripting inquiries are covered.

Chris Turner
wizardfromoz
 

osprey

Active Member
Joined
Apr 15, 2022
Messages
242
Reaction score
140
Credits
2,216
On taking your code and example file, the results on my investigation are at variance with yours:
Here is the code I used, copying from above, and the output:
The script shell:
Code:
[[email protected] ~]$ cat script.sh
#!/bin/bash
char=$(cat $1 | wc -c)
echo "This file has $char characters in it."
nolines=$(cat $1 | tr -d "\n" | wc -c)
echo "This file has $nolines characters not counting the new line."
emptyline=$(grep -cv '\S' $1) echo "This file has $emptyline empty lines."
alphachar=$(tr -cd '[:alpha:]' < $1 | wc -c)
echo "This file has $alphachar alphanumeric characters."

The test file:
Code:
[[email protected] ~]$ cat example_file
This is the first line
This is the second
This has the symbols @#$

The output:
Code:
[[email protected] ~]$ ./script.sh example_file
This file has 67 characters in it.
This file has 64 characters not counting the new line.
This file has  empty lines.
This file has 50 alphanumeric characters.

As you can see, there are significant differences between your results and those here. It could be that there are a number of hidden characters in your test file. One way to check that is to open the test file in vim, and then ask vim to show all possibly hidden characters like spaces and tabs by opening the file in vim and then using the :set list config in vim's command mode. Or you can try and run:

Code:
cat -A example_file
and check the cat manpage for the codes it is using to show hidden characters.
 
Last edited:
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Top