I am new to shell scripting. My aim is to print the numbers like 01, 02, 03...09 if the user enters 1,2,3,4...9 (less than 10). I wrote the below code but lst line print the value is of only single digit.
我是shell脚本的新手。如果用户输入1,2,3,4 ... 9(小于10),我的目的是打印01,02,03 ... 09之类的数字。我写了下面的代码但是lst行打印的值只有一位数。
issuehour()
{
issue_hour="$1";
if [ $issue_hour -lt 10 ] then
h="0"$issue_hour
return $h
else
#echo "less than 10"
return "$1"
fi
}
echo "enter hour"
read hour
hr=$(issuehour "$hour")
echo "after calling function:-" $hr
3 个解决方案
#1
Return statement in bash is synonymous to exit code and ranges from 0 - 255.
bash中的return语句与退出代码同义,范围为0 - 255。
"Echo" is used to return value / output the value of the function. This should be captured by the calling function using $( )
similar to hr=$(issuehour "$hour")
that you have used.
“Echo”用于返回值/输出函数的值。这应该由调用函数使用$()捕获,类似于您使用的hr = $(issuehour“$ hour”)。
Coming to your question, without the echo statement, the function did not return any value to the calling function and that explains why you did not get the desired output. However, since you have used "return", that would have been considered as the 'return code of the function'. To verify this, try:
回到你的问题,没有echo语句,函数没有向调用函数返回任何值,这解释了为什么你没有得到所需的输出。但是,由于您使用了“return”,因此将其视为“函数的返回代码”。要验证这一点,请尝试:
function issuehour()
{
issue_hour="$1";
if [[ $issue_hour -lt 10 ]]
then
h="0"$issue_hour
return $h
else
#echo "less than 10"
return "$1"
fi
}
echo "enter hour"
read hour
hr=$(issuehour "$hour")
echo $?
This will give the same result you were looking for.
这将得到您正在寻找的相同结果。
P.S: It is better to follow the conventional method of using "echo" to return value of the function and "return" statement for "return code"
P.S:最好遵循使用“echo”返回函数值的常规方法和“返回代码”的“return”语句
#2
Just use printf
with 0
padding:
只需使用带有0填充的printf:
printf "%02d\n" 1
01
printf "%02d\n" 2
02
printf "%02d\n" 9
09
printf "%02d\n" 10
10
#3
You can use the following code.
您可以使用以下代码。
issuehour()
{
issue_hour="$1";
if [ $issue_hour -lt 10 ] ;then
h="0"$issue_hour
echo $h
else
#echo "less than 10"
echo "$1"
fi
}
echo "enter hour"
read hour
hr=$(issuehour "$hour")
echo "after calling function:-" $hr
return command will return the exit status of a function , Instead you can use echo command.
return命令将返回函数的退出状态,而不是使用echo命令。
[root@server1 tmp]# bash new
enter hour
10
after calling function:- 10
[root@server1 tmp]# bash new
enter hour
1
after calling function:- 01
#1
Return statement in bash is synonymous to exit code and ranges from 0 - 255.
bash中的return语句与退出代码同义,范围为0 - 255。
"Echo" is used to return value / output the value of the function. This should be captured by the calling function using $( )
similar to hr=$(issuehour "$hour")
that you have used.
“Echo”用于返回值/输出函数的值。这应该由调用函数使用$()捕获,类似于您使用的hr = $(issuehour“$ hour”)。
Coming to your question, without the echo statement, the function did not return any value to the calling function and that explains why you did not get the desired output. However, since you have used "return", that would have been considered as the 'return code of the function'. To verify this, try:
回到你的问题,没有echo语句,函数没有向调用函数返回任何值,这解释了为什么你没有得到所需的输出。但是,由于您使用了“return”,因此将其视为“函数的返回代码”。要验证这一点,请尝试:
function issuehour()
{
issue_hour="$1";
if [[ $issue_hour -lt 10 ]]
then
h="0"$issue_hour
return $h
else
#echo "less than 10"
return "$1"
fi
}
echo "enter hour"
read hour
hr=$(issuehour "$hour")
echo $?
This will give the same result you were looking for.
这将得到您正在寻找的相同结果。
P.S: It is better to follow the conventional method of using "echo" to return value of the function and "return" statement for "return code"
P.S:最好遵循使用“echo”返回函数值的常规方法和“返回代码”的“return”语句
#2
Just use printf
with 0
padding:
只需使用带有0填充的printf:
printf "%02d\n" 1
01
printf "%02d\n" 2
02
printf "%02d\n" 9
09
printf "%02d\n" 10
10
#3
You can use the following code.
您可以使用以下代码。
issuehour()
{
issue_hour="$1";
if [ $issue_hour -lt 10 ] ;then
h="0"$issue_hour
echo $h
else
#echo "less than 10"
echo "$1"
fi
}
echo "enter hour"
read hour
hr=$(issuehour "$hour")
echo "after calling function:-" $hr
return command will return the exit status of a function , Instead you can use echo command.
return命令将返回函数的退出状态,而不是使用echo命令。
[root@server1 tmp]# bash new
enter hour
10
after calling function:- 10
[root@server1 tmp]# bash new
enter hour
1
after calling function:- 01