Jae Nulton
Jae Nulton

Reputation: 391

Using AWK to change a variable located in a script

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.

Upvotes: 0

Views: 178

Answers (2)

tripleee
tripleee

Reputation: 189357

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.

Upvotes: 1

ddoxey
ddoxey

Reputation: 2063

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

Upvotes: 0

Related Questions