ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have been looking all over the web to a simple solution for this , but none of them solved my issue , specially related to a variable and not a file .
The objective is to count the number of digits in a specific variable , and then display one by one .
A quick start
- first i need to count the number of digits in variable
- second i must set a loop with the number of digits and then start displaying them
Code:
#!/bin/bash
#this is the variable
var1=28173
# this is to get the number of digits in that variable
nmbd=$(echo $var1 | awk -F '[0-9]' '{print NF-1}')
# this is the loop
for i in (seq 0 $nmbd);
do
# Thins command was supposedly to read digit by digit
# But is not working
read -n1 out < "$var1"
#This will display the output number by number
# out variable is from the read command
echo $out
done
I have issues in the read command that is not working because it is expecting that $var1 is a file but instead it is a variable .
I have been looking all over the web to a simple solution for this , but none of them solved my issue , specially related to a variable and not a file
Look for Here Strings in the bash man page. But that is probably not how you want to do it in this case.
Quote:
Originally Posted by pedropt
The objective is to count the number of digits in a specific variable , and then display one by one
Looking at your code I conclude that you only need the number of digits in order to iterate over the digits, and that your objective is to output the digits of the number one per line, left to right.
If that is correct then you don't need the count at all and can simply use the shell's built in substitution operators to iterate the number, among other ways. Perhaps something like this (I leave the loop test as an exercise)...
Code:
var=54321X
while [ variable not null ]; do
echo "${var:0:1}"
var=${var#?}
done
Note: I have modified the above to safely handle non-digit characters after seeing NevemTeve's example.
Last edited by astrogeek; 12-24-2017 at 12:51 PM.
Reason: Make subst safe for non-digits
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.