这就是我要做的事情:
我想创建一个可以使用counter_use in
或counter_use out
调用的脚本。如果我输入in
我希望计数器在名为“counter”的文件中为数字值添加+1,如果我输入out
我想从文件中减去1。
如果计数器中的值等于或大于1,我还希望脚本输出Logged in
,如果计数器等于0,我还希望输出Not logged in
。
如果我将计数器硬编码为特定数字,则最后一部分是runnig。问题是第一部分。
echo "In or out?"
read input > counterFile
if grep -q "in" counterFile
then //what should I do here so that it adds +1 to a file called
counter?
elif grep -q "out" counterFile
then //what should I do here so that it subtracts -1 to a file called
counter?
if [ $counter -ge 1 ]
then
echo "Logged in"
elif [ $counter -eq 0 ]
then
echo "Not logged in"
else
echo "Wrong input"
fi
答案 0 :(得分:2)
romaric crailox's helpful answer包含许多有用的指针,但代码可以简化以成为更惯用的(Bash)解决方案:
#!/bin/bash
# Create the counter file on demand.
[[ -f counter ]] || echo 0 > counter
# Prompt until valid input is received.
while read -p 'In or out? ' input; do
case $input in
'in')
# Update the counter file by incrementing the number it contains.
perl -i -ple '++$_' counter
break # valid input received, exit the loop
;;
'out')
# Update the counter file by decrementing the number it contains.
perl -i -ple '--$_' counter
break # valid input received, exit the loop
;;
*)
echo "Invalid input." >&2 # echo error to *stderr*
# Stay in the loop to prompt again.
;;
esac
done
# Read the updated counter file and echo the implied status.
counter=$(< counter)
if (( counter >= 1 )); then
echo 'Logged in.'
elif (( counter == 0 )); then
echo 'Not logged in.'
else
echo "Invalid state: counter is $counter" >&2
fi
注意:
使用case ... esac
比if ... elif ... fi
声明更简洁地处理多个条件。
使用>&2
使用perl
命令更新档案counter
:
-i
激活就地更新-ple
自动p
提示(已修改)输入行,-l
添加智能换行符,-e
指定e
xpression(脚本) run(以下参数)++$_
/ --$_
然后简单地递增/递减手头的输入行($_
),这要感谢-p
,自动输出,并且,由于-i
,写回原始文件(松散地说;创建一个替换原始文件的新文件)。使用算术评估((( ... ))
)来测试数字。
答案 1 :(得分:1)
第一个问题是使用read命令。试试这个
read -p "in or out ?" input
在此命令之后,输入变量将为in
或out
。
然后,您可以使用in
表达式测试输入是out
还是if
:
if [ "$input" == "in" ] [...]
要向计数器文件中的值添加或减去1,您可以检索当前值内部文件,添加或减去1并写入新值内部文件(我不确定您是否要写入新值。请尝试以下操作:
crt_val=$(cat ./counter)
new_val=$(( crt_val + 1 ))
echo $new_val > ./counter
必须存在计数器文件,因此您可以在脚本的开头添加
if [ -e ./counter ] ; then echo "0" > ./counter ; fi
最后,代码可以是这样的:
# user entry
read -p "in or out ? " input
# initialize counter file
if [ ! -e ./counter ] ; then echo "0" > ./counter ; fi
# Doing what user entry ask to do
if [ "$input" == "in" ]
then
echo "in"
crt_val=$(cat ./counter)
new_val=$(( crt_val + 1 ))
echo $new_val > ./counter
elif [ "$input" == "out" ]
then
echo "out"
crt_val=$(cat ./counter)
new_val=$(( crt_val - 1 ))
echo $new_val > ./counter
else
echo "Wrong input, try in or out"
exit 1
fi
# Output
if [ $new_val -ge 1 ]
then
echo "Logged in"
elif [ $new_val -eq 0 ]
then
echo "Not logged in"
else
echo "Wrong counter value"
fi
小心,用户输入必须是in
或out
(区分大小写),并且必须为空。要保护空答案,请尝试if [ "x$input" == "xin" ] [...]
希望这个帮助
答案 2 :(得分:0)
#!/bin/bash
echo "In or out?"
read c <&0 # reads from stdin file descriptor &0 is stdin
if [[ $c == 'In' ]] || [[ $c == 'in' ]];
then
counter=$(<counter.txt) # reads counter from file
let "counter = counter +1" # increases variable
echo "$counter" > 'counter.txt' # write new value to file overwriting the old
elif [[ $c == 'Out' ]] || [[ $c == 'out' ]];
then
counter=$(<counter.txt) # reads from file
let "counter = counter -1"
#echo "$counter" > 'counter.txt'
else
echo "wrong input..."
fi
Write to file, but overwrite it if it exists
https://askubuntu.com/questions/385528/how-to-increment-a-variable-in-bash/706683
http://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php