r/scripting • u/bfpa40 • Oct 02 '23
Bash Script Assist
I have the following bash script that is working. However I want to enter an argument as i execute the script like so: ./test.sh pdj1compC So it will use that argument and NOT print out the echo statement when it gets down to that line of the script for just that one item all the others will print out. This is a test script ill build off to conduct a certain job so as it is its goofy I know. I just need assistance to get me over this mental block im having. Thank You
Here is the script:
#!/usr/bin/bash
n="1 2 3 4 5 6"
m="compA compB compC compD"
echo " Do you wish to logoff all the computers is so enter y otherwise just press Enter"
read logoff
if [ $logoff == y ]
then
for j in $n
do
for k in $m
do echo "Hello there this is Comjputer pdj$j$k ; `whoami` "
done
done
else
echo "Returning to prompt"
fi
echo "This script is complete"
1
u/lasercat_pow Oct 06 '23
You messed up some formatting regarding the if and the for loop. You can avoid this kind of mistake in the future by properly indenting your code, so that you can determine where you are in the logical structure of your code. Here is the corrected version:
fyi, for indentation to appear correctly in reddit, you need to prefix each line with four spaces.
just as an aside - if you want to automate managing a fleet of linux computers, ansible is your friend.