Solving Unix Script Problem: Finding #s w/ 5 1-4321

  • Thread starter Thread starter Bimpo
  • Start date Start date
  • Tags Tags
    Unix
Click For Summary
SUMMARY

The discussion focuses on a Unix script problem aimed at counting how many numbers between 1 and 4321 contain the digit '5'. The user initially struggled with the conditional statement in the script but ultimately resolved the issue by using the correct syntax. The final solution employs a conditional check using the pattern matching feature of Bash to identify numbers containing '5'. The script effectively counts and outputs the total occurrences of such numbers.

PREREQUISITES
  • Bash scripting fundamentals
  • Understanding of conditional statements in Bash
  • Basic knowledge of loops in programming
  • Pattern matching in strings using Bash
NEXT STEPS
  • Explore advanced Bash scripting techniques
  • Learn about regular expressions in Bash
  • Investigate performance optimization in shell scripts
  • Study error handling in Bash scripts
USEFUL FOR

This discussion is beneficial for beginner to intermediate Bash script developers, students tackling Unix scripting assignments, and anyone interested in string manipulation and pattern matching in shell environments.

Bimpo
Messages
9
Reaction score
0

Homework Statement



Trying to find how many numbers contains 5 between 1 to 4321

Homework Equations



n/a

The Attempt at a Solution



#!/bin/bash
typeset -i current=1
typeset -i times=0
while [ $current -le 4321 ] ; do
if [ ] ; then <<<<<<<<<<< stuck here, not sure what to put in if, tried grep etc nonthing works yet
times=times+1
fi
current=current+1
done
echo there are $times numbers that has a 5
 
Physics news on Phys.org
nvm solved it just going to add this for future people whos going to look up this kind of problem in google

if [[ $current = *[5] ]] ; then
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K