4

I am running HP-UX 11.23 and have a bash script that reads two inputs.

Both inputs need to be exactly 6 digits long.

What is the best way to read the input and if its not 6 numbers ask the users to try again ?

echo "Enter the 6 digit missing file numbers"
echo "You don't need to put USSR or .txt.got"
echo
echo "If you just have one file, enter the same number twice"
echo
echo "e.g 230123"
echo " 230130"
echo
echo "Enter the first file name"
read file1
while [ -z "$file1" ]; do
 echo "The first file name is incorrect"
 echo "Please enter 6 digit number again"
 read file1
done
echo
echo "Enter second file name"
read file2
while [ -z "$file2" ]; do
 echo "The last file name is incorrect"
 echo "Please enter 6 digit number again"
 read file2
done
if [ $file1 -le $file2 ]
then
 echo "Sequence numbers look OK" 
fi
AdminBee
23.6k25 gold badges54 silver badges77 bronze badges
asked Sep 11, 2020 at 11:20
5
  • 1
    Do you explicitly want a "6-digit number" in the usual sense -- i.e. 003142 is not acceptable? How about -123456 or +123456. You need two tests: is it 6 long [[ "${#num}" -eq 6 ]] and is it numeric with a RE. Or a RE that explicitly repeats six [0-9] instances. Commented Sep 11, 2020 at 11:29
  • just 6 digits, no plus or minus. Commented Sep 11, 2020 at 11:32
  • This is helpful: unix.stackexchange.com/q/598036 Commented Sep 11, 2020 at 11:36
  • 1
    What scripting language are you using? What do you have so far? Please edit your question and show us your script and specify the language you are using so we don't waste your time with answers that won't help you. Commented Sep 11, 2020 at 11:44
  • 1
    Steve, please don't put updates to your question here in the comments. Instead, edit your question and put them there so everyone can easily find them Commented Sep 11, 2020 at 11:51

1 Answer 1

4
valid() {
 local d='[0123456789]'
 case 1ドル in
 ($d$d$d$d$d$d) true;;
 (*) false;;
 esac
}
# or:
valid() {
 # note that it clobbers $BASH_REMATCH even if you declare
 # it locally to the function.
 local re='^[0123456789]{6}$'
 [[ 1ドル =~ $re ]]
}
until
 IFS= read -rp 'First file: ' file1 || exit
 valid "$file1"
do
 echo >&2 Invalid, try again.
done

Do not use [0-9] in place of [0123456789] unless you're OK for it to also match on characters such as ٠١٢٣٤٥٦٧٨۰۱۲۳۴۵۶۷۸߀߁߂߃߄߅߆߇߈०१२३४५६७८০১২৩৪৫৬৭৮੦੧੨੩੪੫੬੭੮૦૧૨૩૪૫૬૭૮୦୧୨୩୪୫୬୭୮௦௧௨௩௪௫௬௭௮౦౧౨౩౪౫౬౭౮౸౹౺౻౼౽౾೦೧೨೩೪೫೬೭೮൦൧൨൩൪൫൬൭൮෦෧෨෩෪෫෬෭෮๐๑๒๓๔๕๖๗๘໐໑໒໓໔໕໖໗໘༠༡༢༣༤༥༦༧༨༪༫༬༭༮༯༰༱༳၀၁၂၃၄၅၆၇၈႐႑႒႓႔႕႖႗႘፩፪፫፬፭፮፯፰០១២៣៤៥៦៧៨៰៱៲៳៴៵៶៷៸᠐᠑᠒᠓᠔᠕᠖᠗᠘᥆᥇᥈᥉᥊᥋᥌᥍᥎᧐᧑᧒᧓᧔᧕᧖᧗᧘᧚᪀᪁᪂᪃᪄᪅᪆᪇᪈᪐᪑᪒᪓᪔᪕᪖᪗᪘᭐᭑᭒᭓᭔᭕᭖᭗᭘᮰᮱᮲᮳᮴᮵᮶᮷᮸᱀᱁᱂᱃᱄᱅᱆᱇᱈᱐᱑᱒᱓᱔᱕᱖᱗᱘0456780123456781⁄71⁄91⁄101⁄32⁄31⁄52⁄53⁄54⁄51⁄65⁄61⁄83⁄85⁄87⁄81⁄ↅ0⁄3123456781011121314151617181920(1)(2)(3)(4)(5)(6)(7)(8)(10)(11)(12)(13)(14)(15)(16)(17)(18)(19)(20)1.2.3.4.5.6.7.8.10.11.12.13.14.15.16.17.18.19.20.0⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴⓵⓶⓷⓸⓹⓺⓻⓼⓾⓿❶❷❸❹❺❻❼❽❿➀➁➂➃➄➅➆➇➉➊➋➌➍➎➏➐➑➓〇〡〢〣〤〥〦〧〨㉈㉉㉊㉋㉌㉍㉎㉏2122232425262728293031323334353637383940414243444546474849501月2月3月4月5月6月7月8月10月11月12月0点1点2点3点4点5点6点7点8点10点11点12点13点14点15点16点17点18点19点20点21点22点23点24点1日2日3日4日5日6日7日8日10日11日12日13日14日15日16日17日18日19日20日21日22日23日24日25日26日27日28日29日30日31日꘠꘡꘢꘣꘤꘥꘦꘧꘨꣐꣑꣒꣓꣔꣕꣖꣗꣘꤀꤁꤂꤃꤄꤅꤆꤇꤈꧐꧑꧒꧓꧔꧕꧖꧗꧘꧰꧱꧲꧳꧴꧵꧶꧷꧸꩐꩑꩒꩓꩔꩕꩖꩗꩘꯰꯱꯲꯳꯴꯵꯶꯷꯸012345678𐄇𐄈𐄉𐄊𐄋𐄌𐄍𐄎𐅂𐅃𐅈𐅏𐅘𐅙𐅚𐅛𐅜𐅝𐅞𐅟𐅳𐆊𐋡𐋢𐋣𐋤𐋥𐋦𐋧𐋨𐌠𐌡𐏑𐏒𐒠𐒡𐒢𐒣𐒤𐒥𐒦𐒧𐒨𐡘𐡙𐡚𐡹𐡺𐡻𐡼𐡽𐢧𐢨𐢩𐢪𐢫𐢬𐣻𐣼𐤖𐤚𐤛𐧀𐧁𐧂𐧃𐧄𐧅𐧆𐧇𐩀𐩁𐩂𐩃𐩽𐪝𐫫𐫬𐭘𐭙𐭚𐭛𐭸𐭹𐭺𐭻𐮩𐮪𐮫𐮬𐳺𐳻𐹠𐹡𐹢𐹣𐹤𐹥𐹦𐹧𑁒𑁓𑁔𑁕𑁖𑁗𑁘𑁙𑁦𑁧𑁨𑁩𑁪𑁫𑁬𑁭𑁮𑃰𑃱𑃲𑃳𑃴𑃵𑃶𑃷𑃸𑄶𑄷𑄸𑄹𑄺𑄻𑄼𑄽𑄾𑇐𑇑𑇒𑇓𑇔𑇕𑇖𑇗𑇘𑇡𑇢𑇣𑇤𑇥𑇦𑇧𑇨𑋰𑋱𑋲𑋳𑋴𑋵𑋶𑋷𑋸𑑐𑑑𑑒𑑓𑑔𑑕𑑖𑑗𑑘𑓐𑓑𑓒𑓓𑓔𑓕𑓖𑓗𑓘𑙐𑙑𑙒𑙓𑙔𑙕𑙖𑙗𑙘𑛀𑛁𑛂𑛃𑛄𑛅𑛆𑛇𑛈𑜰𑜱𑜲𑜳𑜴𑜵𑜶𑜷𑜸𑣠𑣡𑣢𑣣𑣤𑣥𑣦𑣧𑣨𑱐𑱑𑱒𑱓𑱔𑱕𑱖𑱗𑱘𑱚𑱛𑱜𑱝𑱞𑱟𑱠𑱡𒐀𒐁𒐂𒐃𒐄𒐅𒐆𒐈𒐉𒐊𒐋𒐌𒐍𒐏𒐐𒐑𒐒𒐓𒐕𒐖𒐗𒐘𒐙𒐚𒐛𒐜𒐞𒐟𒐠𒐡𒐢𒐣𒐤𒐥𒐦𒐧𒐨𒐩𒐪𒐬𒐭𒐮𒐯𒐰𒐱𒐴𒐵𒐶𒐷𒐸𒐹𒐺𒐻𒐼𒐽𒐾𒐿𒑀𒑁𒑂𒑃𒑄𒑅𒑊𒑋𒑌𒑍𒑎𒑏𒑐𒑑𒑒𒑓𒑔𒑕𒑖𒑗𒑘𒑙𒑩𒑪𒑫𒑬𒑭𖩠𖩡𖩢𖩣𖩤𖩥𖩦𖩧𖩨𖭐𖭑𖭒𖭓𖭔𖭕𖭖𖭗𖭘𝍠𝍡𝍢𝍣𝍤𝍥𝍦𝍧012345678012345678012345678012345678012345678𞣇𞣈𞣉𞣊𞣋𞣌𞣍𞣎𞥐𞥑𞥒𞥓𞥔𞥕𞥖𞥗𞥘0.0,1,2,3,4,5,6,7,8,🄋🄌🆛🆜🆝🆞🆟🆠🆡🆢🆣🆤 depending on the locale and system.

answered Sep 11, 2020 at 12:39
2
  • 1
    Entirely unrelated to the question itself, I've never seen the until list-1; do...done syntax like this before (more commonly, nor for while). I'd always assumed it was until/while command1 [&&/|| command2...]; do ... done. Thank you for teaching me something new today Commented Sep 11, 2020 at 12:50
  • 1
    @roaima same here, it is one of the reason I come and stay here (U&L), I can learn new (dirty cough) trick. Commented Sep 11, 2020 at 13:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.