Linux

All about Linux kernel and operating systems that are based on it.
[Random post]

 tags  gallery  RSS

16.5.2022 19:04:17

A quick fight game made in bash

#!/bin/bash
health=10
enemy=10
while [[ $health > 0 && $enemy > 0 ]]
do
echo You: $health
echo Enemy: $enemy
input=
while [[ $input != 0 && $input != 1 ]]
do
echo "Pick a number (0-1)"
read input
if [[ $input != 0 && $input != 1 ]]; then
echo "Wrong input"
fi
done
hit=$(( $RANDOM % 2 ))
if [[ $input == $hit ]]; then
((enemy--))
else
((health--))
fi
done
if [[ $health == 0 ]]; then
echo "You Died"
else
echo "You defeated the beast"
fi

Save this as a file named fight.sh onto any linux system, chmod +x it, run it and have fun

19.2.2022 21:50:31

Double dollar sign ($$) in bash

It substitutes to PID of the current running shell. For example running

ps -Alf|grep $$

generates interesting results, as shown below

$ ps -Alf|grep $$
0 S juozas     11125   11120  0  80   0 -  3391 do_wai 22:43 pts/0    00:00:00 bash
4 R juozas     11197   11125  0  80   0 -  3506 -      22:46 pts/0    00:00:00 ps -Alf
0 S juozas     11198   11125  0  80   0 -  2859 pipe_w 22:46 pts/0    00:00:00 grep --color=auto 11125

Another example is to use it in the file names where it gets substituted with the PID of the current running shell.

$ cat >f$$.txt <<EOF
This is a test. The pid of running shell is $$
EOF
$ cat f$$.txt
This is a test. The pid of running shell is 11125
$ ls f$$.txt
f11125.txt
$ LC_ALL=C rm -v f$$.txt
removed 'f11125.txt'

reposted by juozaspo
 

Timeline speed: 0.71 posts per month

 

You have reached the end...

THE END

login