I am trying to add to a script I'm writing a restriction to the input where the user can only input 9 or 10 characters I set it up like this but when I run it, it doesn't work the way I want it to. Everything I type in comes back as 10 characters even if I just put one number. What is wrong with my code?
#!/bin/bash
#
echo "Please input numbers only"
read inputline
if read -n10 inputline
then
echo "10 chars"
else
if read -n9 inputline
then
echo "9 chars"
else
echo "invalid input length"
fi
Your script is asking for input three times. I'm assuming that the following is closer to what you intend:
#!/bin/bash
read -p "Please input numbers only " inputline
len=${#inputline}
if (( len == 9 || len == 10 ))
then
echo "$len chars"
else
echo "invalid input length"
fi
The -n option to read limits the input to the specified number of characters but accepts that length without pressing enter. You can enter fewer by pressing Enter though. I've found it to be useful for -n 1 but rarely otherwise.
Use the offset Parameter Expansion to truncate whatever they enter to a max of 10 chars like so:
$ num="123456789012"; echo ${num::10}
1234567890
Related
I am trying to count the seize of an parameter without the numbers and spaces, like if someone types in "Hello player1" it has to echo "11 characters". I have tried using ${#value} but this counts numbers and spaces.
if [ -z "$1" ]
then
echo "write at least 1 word"
else
for value in "$#"
do
echo "${value//[[:digit:]]/}"
done
echo ${#value}
fi
The result of the code
as you can see in the image it counts only the last parameter and counts the numbers what I don't want
Results of the second code
Break it into two steps.
set -- "abc 123" "d12"
for value do # in "$#" is default, don't need to say it
valueTrimmed=${value//[[:digit:][:space:]]/}
echo "The string '$value' has ${#valueTrimmed} valid characters"
done
...properly emits as output:
The string 'abc 123' has 3 valid characters
The string 'd12' has 1 valid characters
bash does not support nesting parameter expansions; you cannot do this in one step as a shell builtin (it could be done in a pipeline, but only with an unreasonably high performance cost).
If I press the L key, the line will move to the left. If I press R key, the line will move to the right.
I managed to do this:
#!/bin/bash
b=" * "
while :
do
echo "$b"
read -s input
if [ $input==s ]
then
echo "${b:1}"
else
if [ $input==d ]
then
echo " $b"
fi
fi
sleep 1
done
But I don't know how to do the input control without inserting "read" and blocking the cycle.
If you sleep one second anyway, you can also use the timeout option of read.
This will read up to one second for input:
read -t 1 input
When the return value of read is higher than 128, the timeout has been reached. If it is lower, you have to wait.
if (( $? <= 128 )); then sleep 1; fi
By the way: this prints a star in the middle of the screen:
printf "%*s\n" $((COLUMNS / 2)) \*
COLUMNS is a variable, which contains the screen width. The number in front of the s format option says that the string should be aligned by that much spaces. And if you specify a star instead of a number printf reads the width from the command line.
is there a way I can limit bashs read to only accept numeric input,
so when anything else then a number is added, the user gets promted again?
read -r -p "please enter 2 numbers: " number
Use a loop with a condition using a pattern:
#!/bin/bash
unset number
until [[ $number == +([0-9]) ]] ; do
read -r -p "please enter a number: " number
done
echo $((number + 1))
You might need to be more precise (#(0|#([1-9])*([0-9]))) if you want to use the number directly, because e.g. 09 will cause an error, as it will be interpreted as octal because of the starting 0.
change all none numeric value to empty and check for length.
if [[ -n ${number//[0-9]/} ]]; then
echo "please enter a numeric value!"
fi
I'm writing a bash script as the first part of an assignment. If the number of arguments is two, it's supposed to return the sum; if it's anything but two, it's supposed to return the error message and exit the script.
But even when I enter two commands, it still gives me the error message. Why is that? I wrote something very similar -- subtracting numbers -- a second ago and it worked fine.
#!/bin/bash
# This script reads two integers a, b and
# calculates the sum of them
# script name: add.sh
read -p "Enter two values:" a b
if [ $# -ne 2 ]; then
echo "Pass me two arguments!"
else
echo "$a+$b=$(($a+$b))"
fi
read reads from standard input, while the arguments ($1, $2, ...) whose count you are checking with $# are command line arguments that can be passed to your program when it is called.
I'd suggest
read -p "Enter two values: " a b additional_garbage
if [[ -z $b ]]; then # only have to test $b to ensure we have 2 values
The "additional_garbage" is to guard against the funny user who enters more than 2 values, and then $b is something like "2 3 4" and your arithmetic is broken.
And to guard against invalid octal numbers (for example if the user enters 08 and 09), enforce base-10
echo "$a+$b=$(( 10#$a + 10#$b ))"
#!/bin/bash
echo "Please enter a number that can divided by 5"
read input
ans= 'expr $input/5'
echo "$ans"
So i want this code to be something like this, a user will enter a number that can divided by 5 (5,10,15,20 etc etc) and then it will echo the answer, i am not sure whats wrong with my code it keeps saying "no such file or directory " for output not sure how to fix that.
I think the spaces were your problem...
#!/bin/bash
echo "Please enter a number that can divided by 5"
read input
ans=`expr $input / 5`
echo "$ans"