Reputation: 89
How can I randomly run for loop in sh of git action normally?
There is a github repository with daily commits here.
for github action
The yml file below exists.
name: planting-grass
on:
schedule:
- cron: '0 0 * * *'
jobs:
task:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set current date
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
- name: Execute commands
run: sh ./task.sh ${{ steps.date.outputs.date }}
- name: Commit files
run: |
git config --global user.name "$(git --no-pager log --format=format:'%an' -n 1)"
git config --global user.email "$(git --no-pager log --format=format:'%ae' -n 1)"
git add date.txt
git commit -m ${{ steps.date.outputs.date }}
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}
This yml runs task.sh. task.sh looks like this:
#!/bin/bash
echo "DATE: $1" >> date.txt
everything works fine.
However, in data.txt, one line per day is accumulated.
I want to accumulate lines between 1 and 100 in data.txt.
Where can I edit to use the above function?
Then I tried the following:
#!/bin/bash
range=100
number=$((RANDOM % range))
while [ "$i" -le "$number" ]; do
echo "DATE: $1" >> date.txt
done
#!/bin/bash
range=100
number=$((RANDOM % range))
i=0
while (( i <= number )); do
echo "DATE: ${i}" >> date.txt
i=$((i+1))
done
#!/bin/bash
range=100
number=$((RANDOM % range))
for i in $(seq 0 $number); do
echo "DATE: ${i}" >> date.txt
done
But all failed.
It outputs the following error message:
./task.sh: 7: [: Illegal number:
./task.sh: 7: [[: not found
./task.sh: 7: i: not found
I wonder how can I run a normal random for loop.
Best Regards!
EDIT)
#!/bin/bash
range=100
number=$((RANDOM % range))
i=0
while (( i <= number )); do
echo "DATE: ${i}" >> date.txt
i=$((i+1))
done
The above code works fine, but it only works once.
Unlike wanting to run echo "DATE: ${i}" >> date.txt
randomly between 1 and 100, it is always executed only once. How can we resolve this problem?
Upvotes: 2
Views: 950
Reputation: 5241
You can change run: sh ./task.sh
to run: bash ./task.sh
to run your script with bash, instead of sh
.
If your script is executable (chmod a+x task.sh
, and commit the file), and you have the right shebang (#!/bin/bash
), you can also do run: ./task.sh
.
Your cron schedule (0 0 * * *
) is set to run daily. If you want to run more frequently, you could change it. For example 0,30 * * * *
will run every half an hour (the date will be the same all day unless you include the time, eg date + '%Y-%m%d_%H:%M:%S'
).
Upvotes: 2