I want to loop through all the files in the directory and check their perms (if user wants) but for some reason I'm getting this error:
./perms.sh: line 12: syntax error near unexpected token `;;'
./perms.sh: line 12: ` r) if [ -r $i ] then echo 'True' else echo 'False' fi ;;'
here is my code:
#!/bin/bash
for i in *
do
echo "Do you want to check rights for $i (y/n)"
read marzi
if [ $marzi = 'y' ]
then
echo 'which commands to check? '
read check
case $check in
r) if [ -r $i ] then echo 'True' else echo 'False' fi ;;
w) if [ -w $i ] then echo 'True' else echo 'False' fi ;;
x) if [ -x $i ] then echo 'True' else echo 'False' fi ;;
*) echo 'unrecognized!' ;;
esac
else
echo "skipped $i"
fi
done
does this have something to do with apostrophe?
maybe correct inline if format should be
if [ -r $i ]; then echo 'True'; else echo 'False'; fi
make sure to make change for r and w and x
Related
I have the following problem.
How can I loop trough a .csv file and do an something when the ID of $d is a certain number.
I made one with an if elseif like this that works fine:
while read a b c d e
do
if [ "$d" = "20.000" ]
then
ACTION
elif [ "$d" = "24.000" ]
then
ACTION
elif [ "$d" = "28.000" ]
then
ACTION
elif [ "$d" = "32.000" ]
then
ACTION
elif [ "$d" = "53.000" ]
then
ACTION
elif [ "$d" = "8.000" ]
then
ACTION
elif [ "$d" = "36.000" ]
then
ACTION
elif [ "$d" = "37.000" ]
then
ACTION
Now this code becomes very long and was looking for a nicer solution.
I have the following, but I get the following errors.
line 24: syntax error near unexpected token of' line 24: case $d of'
Here is my code:
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read a b c d e
#a=name
#b=northing
#c=easting
#d=id
#e=color
do
read d
case $d of
exit) break ;;
20.000) echo "NBUOY" ;;
24.000) echo "EBUOY" ;;
28.000) echo "SBUOY" ;;
32.000) echo "WBUOY" ;;
53.000) echo "RBUOY" ;;
8.000) echo "RBUOY" ;;
36.000) echo "RBUOY" ;;
37.000) echo "RBUOY" ;;
55.000) echo "RBUOY" ;;
10.000) echo "RBUOY" ;;
14.000) echo "GBUOY" ;;
16.000) echo "GBUOY" ;;
6.000) echo "YSBUOY" ;;
4.000) echo "YSBUOY" ;;
1.000) echo "YSBUOY" ;;
5.000) echo "YSBUOY" ;;
*) echo "Unknown response, enter a number 1-12 or type 'exit' to quit" ;;
esac
done
What is going wrong? and is this even possible?
Restricting user from trying multiple invalid attempt in shell scripting. I wrote the below script but somehow it's not getting me desire output. I have shared the script and script output both. Kindly help. Here I wanted script to terminate if user tried more than 3 times.
While true
do
echo -n "Enter yes or no"
read opt
case $opt in
yes) break ;;
no) break ;;
*) echo "Invalid input"
while [[ $err -le 3 ]]
do
If [[ $err -le 3 ]]
then
echo "err: $err"
((err++))
break
else
echo "Max limit crossed"
exit 1
fi
done
;;
esac
done
This was a nice question and I had a lot of fun solving it. I have to mention that I'm new to shell programming.
n=0
until [ $n -ge 3 ]
do
read line
if [ "$line" = "XYZ" ]; then
echo "Accepted"
break
else
n=$[$n+1]
echo " trying " $n "times "
fi;
done
This article helped me a lot to solve it.
Try:
#!/bin/bash
ANSWER=
max=3
while true; do
echo "Enter yes or no:"
read -r ANSWER
[[ $ANSWER == "yes" || $ANSWER == "no" ]] && break
echo Invalid Input
ANSWER=
((--max))
[[ $max -le 0 ]] && { echo "Max limit crossed"; exit 1; }
done
Team,
I am not able to figure out what is the issue with my indentation or syntax. can any one hint? I tried it on linux and i get below error:
error: output
line 23: syntax error near unexpected token `['
line 23: ` if [ $CLUSTER_NAME == prod.$test_environment ]; then'
#!/bin/bash
sops_ops() {
sops --version
if [ "$?" -eq "0" ]; then
echo "proceed sops ops"
else
echo "check sops binary"
fi
read -p 'Enter cluster_NAME: ' CLUSTER_NAME
test_environment="test.nvaa.com"
test1_environment="test1.nvaa.com"
case "${$CLUSTER_NAME}" in
prod.$test_environment) ;;
dev.$test1_environment) ;;
*) echo "Invalid option: ${CLUSTER_NAME}" 1>&2 && exit 1 ;;
if [ $CLUSTER_NAME == prod.$test_environment ]; then
printf "got test cluster $CLUSTER_NAME"
elif [ $CLUSTER_NAME == dev.$test1_environment ];then
printf "got test1 cluster $NAME"
echo "not found cluster"
else
echo "Environment not available"
fi
}
sops_ops
You're are missing esac. Add it after the following snippet :
case "${$CLUSTER_NAME}" in
prod.$test_environment) ;;
dev.$test1_environment) ;;
*) echo "Invalid option: ${CLUSTER_NAME}" 1>&2 && exit 1 ;;
Here is my code:
#!/bin/bash
echo "Letter:"
read a
if [ $a = "a" ]
then
echo "LOL"
fi
if [ $a = "b" ]
then
echo "ROFL"
fi
Is there a way for me to loop this so that, after displaying either LOL or ROFL, I would be asked for a letter again?
Yes.
Oh, you want to know how?
while true; do
echo "Letter:"
read a
if [ $a = "a" ]
then
echo "LOL"
elif [ $a = "b" ]
then
echo "ROFL"
fi
done
Of course, you probably want some way to get out of that infinite loop. The command to run in that case is break. I would write the whole thing like this:
while read -p Letter: a; do
case "$a" in
a) echo LOL;;
b) echo ROFL;;
q) break;;
esac
done
which lets you exit the loop either by entering 'q' or generating end-of-file (control-D).
Don't forget that you always want -r flag with read.
Also there is a quoting error on that line:
if [ $a = "a" ] # this will fail if a='*'
So here is a bit better version(I've also limited the user to input only 1 character):
#!/bin/bash
while true; do
read -rn1 -p 'Letter: ' a
echo
if [[ $a = 'a' ]]; then
echo "LOL"
elif [[ $a = 'b' ]]; then
echo "ROFL"
else
break
fi
done
Or with switch statement:
#!/bin/bash
while read -rn1 -p 'Letter: ' a; do
echo
case $a in
a) echo LOL;;
b) echo ROFL;;
*) break;;
esac
done
I have a test script the needs to read the variable 'LAB' and e-mail the correct company.
I've looked but can't find anything that has worked yet.
Any thoughts?
#!
#
LAB=3
#
if [ "$LAB" = "$1" ];then
echo "Got Zumbrota" && ./mailZ
fi
#
if [ "$LAB" = "$2" ];then
echo "Got Barron" && ./mailB
fi
#
if [ "$LAB" = "$3" ];then
echo "Got Stearns" && ./mailS
fi
If this a bash script, start your file with
#!/bin/bash
and use -eq for integer comparison and since LAB is an integer in your script
if [ $LAB -eq $1 ]
These cascading if statements can be condensed into a case statement:
case "$LAB" in
1) echo "Got Zumbrota" && ./mailZ
;;
2) echo "Got Barron" && ./mailB
;;
3) echo "Got Stearns" && ./mailS
;;
*) echo "don't know what to do with $LAB"
;;
esac