1

I am trying to get the contents of a txt file "usernum.txt", get the contents, add one to that number, and replace the current number with the new one. Here is what I have so far

#!/bin/sh ID=0 cat ''$pwd'usernum.txt' >> $ID count1=1 IDB=$(($ID+$count1)) rm "usernum.txt" touch "usernum.txt" echo $IDB >> usernum.txt 

It runs but when I open the file, it stays the same. Any ideas? Thanks, Mike

1

1 Answer 1

3

You should store the output from cat into ID instead of using >>. The way you are doing that you always add to a file named '0'. To store the output of cat use:

ID=$( cat 'usernum.txt' ) 

I have also removed the 'pwd' as you do not need it and also it gets escaped by the single quotes.

EDIT: here is a complete working example. Note that if there is no usernum.txt file this will print some errors(cat and rm fail) but still will work(i.e. will print 1 in the file). You should perform a check to see if the file exists to avoid these errors:

ID=$( cat 'usernum.txt' ) count1=1 IDB=$(($ID+$count1)) rm "usernum.txt" touch "usernum.txt" echo $IDB >> usernum.txt 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.