2017-04-10 67 views
1

减去1这是我想要做的事:bash脚本 - 添加/从文件

我想打一个脚本,我可以调用要么counter_use incounter_use out。如果我输入in我想要一个计数器给名为“counter”的文件中的数字值加+1,如果我输入out,我想从文件中减去1。

我还希望脚本输出Logged in如果在计数器中的值是等于或大于1和Not logged in更高如果计数器等于0

如果我硬编码计数器的最后一部分运行过程中出现一个具体的数字。问题是第一部分。

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 

回答

1

第一个问题是使用读命令。试试这个

read -p "in or out ?" input 
  • -p选项是specifie东西打印到标准输出
  • 输入shell脚本变量将存储用户输入

此命令后的名称,输入变量将是inout。 然后你可以测试是否输入inoutif表达:

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 

要当心,用户条目必须是准确inout(区分大小写),并且必须是没有空。为了保护空的答案,尝试if [ "x$input" == "xin" ] [...]

希望这有助于

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

+0

这并不是真的可靠计数,因为你有一个竞争条件。您需要在更新期间锁定计数器文件。 – user1934428

+0

你能解释一下吗? –

+0

您读取计数器文件,稍后再编写更新的计数器文件。如果第二个进程同时运行此脚本,它可以读取您的阅读和写作之间的计数器文件,因此不会看到您的更新值。 – user1934428

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声明。

  • 1.4.3错误消息发送到stderr的>&2

  • 使用perl命令的到位,以更新文件counter

    • -i激活就地更新
    • -ple自动p rints (修改后的)输入行-l增加了intell igent换行符吊运和-e指定e XPRESSION(脚本)来运行(以下参数)
    • ++$_/--$_然后简单地递增/递减手头的输入线($_),其由于-p,自动获取输出,并且,由于-i,写回到原始文件(松散地说;将创建一个替换原始文件的新文件)。
  • 使用算术评估(((...)))来测试数字。

+1

感谢这个更好的解决方案 –