0

This is the bash script.
Counter.sh:

#!/bin/bash
rm -rf home/pi/temp.mp3
cd /home/pi/
now=$(date +"%d-%b-%Y")
count="countshift1.sh"
mkdir $(date '+%d-%b-%Y')


On row 5 of this script, the count variable... I just want to know how to use AWK to change the integer 1 (the 18th character, thanks for the response) into a 3 and then save the Counter.sh file.

Jae Nulton
  • 373
  • 1
  • 6
  • 13
  • 2
    IMHO, your description of your goal is unclear. It would be completely **un**ambiguous if you included as sample of data to be processed by `countshift.sh` and the expected output from that input. Consider editting your Q to inlcude these definitions. Also, I'm guess you want `count=$(countshift1.sh)` which is command-substitution and will allow the assignment of any returned output from `countshift1.sh` to the variable `count`. Good luck. – shellter Jun 27 '16 at 03:57
  • 1
    This is an awful idea. The code in the script should be static. You could have it read in a value from an external file, though. – tripleee Jun 27 '16 at 04:04
  • 1
    For the record, see also now http://stackoverflow.com/questions/38044884/time-variables-in-bash-scripting – tripleee Jun 27 '16 at 04:05
  • I removed the explanation so that the question was more clear. The other script nothing to process, hence that was not posted. Countshift1.sh is merely a counter that incrementally increases each time this script is run. I merely want the filename to use a variable, but I need awk to change the variable name because tracking stats by time is too complex for me to figure out currently. Thank you for the suggestion. – Jae Nulton Jun 27 '16 at 04:08
  • 1
    1 is the 18th character in `count="countshift1.sh"` not the 17th. – user3439894 Jun 27 '16 at 04:12

2 Answers2

1

This is basically http://mywiki.wooledge.org/BashFAQ/050 -- assuming your script actually does something with $count somewhere further down, you should probably refactor that to avoid this antipattern. See the linked FAQ for much more on this topic.

Having said that, it's not hard to do what you are asking here without making changes to live code. Consider something like

awk 'END { print 5 }' /dev/null > file

in a cron job or similar (using Awk just because your question asks for it, not because it's the best tool for this job) and then in your main script, using that file;

read index <file
count="countshift$index.sh"

While this superficially removes the requirement to change the script on the fly (which is a big win) you still have another pesky problem (code in a variable!), and you should probably find a better way to solve it.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

I don't think awk is the ideal tool for that. There are many ways to do it.

I would use Perl.

perl -pi -e 's/countshift1/countshift3/' Counter.sh
ddoxey
  • 2,013
  • 1
  • 18
  • 25