I am trying to write a script and at some point i needed to list all the things in a directory and grep someting from that directory. I can't do it with ls. ls is not my guy to do it. So i tried to do ls' job with echo command instead , but it is giving me a permission denied error now. You can see the script below :
#!/bin/sh
# Test for network connection
for interface in $(echo $(/sys/class/net/*) | grep -v lo);
do
if [ "$(cat /sys/class/net/"$interface"/carrier)" = 1 ]; then
OnLine=1;
fi
done
if ! [ $OnLine ]; then echo "Not Online" > /dev/stderr;
exit;
fi
And i am getting this error :
./carriercontrol.sh: line 10: /sys/class/net/apcli0: Permission denied
What can i do to complete this script ? Is there a way to get the listing of a directory and pipe it with something. Also even i could get rid of that permission then i think echo will cause more trouble to me.
EDIT : I tried to replace echo with find command , here is the results and errors.
#!/bin/sh
# Test for network conection
for interface in $(find /sys/class/net -mindepth 1 | grep -v lo);
do
if [ "$(cat /sys/class/net/"$interface"/carrier)" = 1 ]; then
OnLine=1;
fi
done
if ! [ $OnLine ]; then echo "Not Online" > /dev/stderr;
exit;
fi
cat: can't open '/sys/class/net//sys/class/net/ra0/carrier': No such file or directory
cat: can't open '/sys/class/net//sys/class/net/eth0/carrier': No such file or directory
cat: can't open '/sys/class/net//sys/class/net/br-lan/carrier': No such file or directory
cat: can't open '/sys/class/net//sys/class/net/eth0.1/carrier': No such file or directory
cat: can't open '/sys/class/net//sys/class/net/apcli1/carrier': No such file or directory
cat: can't open '/sys/class/net//sys/class/net/apcli0/carrier': No such file or directory
find
command? – Ipor Sircer Jul 27 '18 at 10:24$(/sys/class/net/*)
is a command substitution - it is trying to execute the files in/sys/class/net
– steeldriver Jul 27 '18 at 10:26for interface in /sys/class/net/*
and check for / excludelo
within the body of the loop – steeldriver Jul 27 '18 at 10:30echo
command, have already been asked here. See https://unix.stackexchange.com/questions/126009/ , https://unix.stackexchange.com/questions/147030/ , https://unix.stackexchange.com/questions/353179/ , and https://unix.stackexchange.com/a/166659/5132 for starters. – JdeBP Jul 27 '18 at 11:33