I tried this, but no luck, it always says "not ready":
Code: Select all
export MOUNT=/dev/loop0
if grep -qs $MOUNT /proc/mounts ; then
echo "loop0 device is ready! Command status: $?"
else
echo "$MOUNT is not ready!"
fi
Code: Select all
export MOUNT=/dev/loop0
if grep -qs $MOUNT /proc/mounts ; then
echo "loop0 device is ready! Command status: $?"
else
echo "$MOUNT is not ready!"
fi
Code: Select all
MOUNT="loop0"
mount | grep $MOUNT && T="" || T=" not" ; echo "$MOUNT device is$T ready !"
Not so much to explain, but in short:PsySc0rpi0n wrote:Could you please explain the code ?
the command1 && command2 || command3 kindof takes care of that:PsySc0rpi0n wrote:And no "if" statement is needed?
Code: Select all
MOUNT="loop0"
T=" not";mount|grep $MOUNT && T=;echo "$MOUNT device is$T ready !"
Code: Select all
case $opt in
a|A)
make_available()
;;
u|U)
make_unavailable()
;;
x|X)
exit 1
;;
*)
echo "Unknown error!" >&2
;;
esac
Code: Select all
case $opt in
"a"|"A")
make_available()
;;
"u"|"U")
make_unavailable()
;;
"x"|"X")
exit 1
;;
*)
echo "Unknown error!" >&2
;;
esac
====The case statement is one of the most difficult commands to indent clearly, and people frequently ask about the most "correct" style. Just do your best - there are many variations of indenting style for case and no real agreed-upon best practice.
So maybe start out with some simpler scripts, and learn more, before tryingAnd I'm completely new to bash scripting so maybe----snip---
Also, there are better alternatives then "bash" , some people believe that bash stands for "broken again shell" with despicable security record.Portability considerations
Only the ;; delimiter is specified by POSIX.
zsh and mksh use the ;| control operator instead of Bash's ;;&. Mksh has ;;& for Bash compatability (undocumented).
ksh93 has the ;& operator, but no ;;& or equivalent.
ksh93, mksh, zsh, and posh support a historical syntax where open and close braces may be used in place of in and esac: case word { x) …; };. This is similar to the alternate form Bash supports for its for loops, but Bash doesn't support this syntax for case..esac.
That's subjective. Bash is the most popular, and can be used to most scripting needs.GarryRicketson wrote:
Also, there are better alternatives then "bash" , some people believe that bash stands for "broken again shell" with despicable security record.
you really need to SHOW us!PsySc0rpi0n wrote:Why is this "case esac" returning syntax errors at the first ";;":
Code: Select all
# define function
do_this() {
this
}
# call it with
do_this
# and not do_this()
Code: Select all
MOUNT="loop0"
df|awk '{print $1" is mounted on " $6 " !";}'|grep $MOUNT||echo -e "\033[1;31m$MOUNT is not mounted !"
I can't see anything in that link talking about indentation!GarryRicketson wrote:Why is this "case esac" returning syntax errors at the first ";;":
Maybe,first hit: http://wiki.bash-hackers.org/syntax/ccmd/caseThe case statement is one of the most difficult commands to indent clearly, and people frequently ask about the most "correct" style. Just do your best - there are many variations of indenting style for case and no real agreed-upon best practice.
It's the most basic script I get get other than ---echo "Hello World!".GarryRicketson wrote: ====So maybe start out with some simpler scripts, and learn more, before tryingAnd I'm completely new to bash scripting so maybe----snip---
more advanced things. It is hard to explain trigonometry to a math student that has not yet learned to add, subtract ,divide, multiply, etc.....
Ok, thanks for replying!GarryRicketson wrote:Also, there are better alternatives then "bash" , some people believe that bash stands for "broken again shell" with despicable security record.Portability considerations
Only the ;; delimiter is specified by POSIX.
zsh and mksh use the ;| control operator instead of Bash's ;;&. Mksh has ;;& for Bash compatability (undocumented).
ksh93 has the ;& operator, but no ;;& or equivalent.
ksh93, mksh, zsh, and posh support a historical syntax where open and close braces may be used in place of in and esac: case word { x) …; };. This is similar to the alternate form Bash supports for its for loops, but Bash doesn't support this syntax for case..esac.
I started with C when I started my (late) graduation! I know the basics of C.arzgi wrote:That's subjective. Bash is the most popular, and can be used to most scripting needs.GarryRicketson wrote:
Also, there are better alternatives then "bash" , some people believe that bash stands for "broken again shell" with despicable security record.
I'd suggest also learning some general programming language later, I would recommend python.
I do 'mixing', call bash scripts from python programs, python programs from bash scripts. Also sed and awk in some programs. Each language has it's own strenghts.
I tried the script with the '()?...debiman wrote:you really need to SHOW us!PsySc0rpi0n wrote:Why is this "case esac" returning syntax errors at the first ";;":
but in this case i'm fairly sure: functions are not called with () at the end.Code: Select all
# define function do_this() { this } # call it with do_this # and not do_this()
Code: Select all
command not found!
I think you are correct! I previously said I wanted to check if a filesystem was mounted but in fact, it was rather if a file has been successfully attached to loop0 or not. By terminal command this is done using "sudo losteup /dev/loop0 file.ext". What I want was to check if this command was successful or not. For that I ended up using the return value of the command itself.L_V wrote:@PsySc0rpi0n
I think the priority is to 100% clarify in your mind what you want to achieve before looking for a solution (which is not unique).
You can investigate this for test purpose:Code: Select all
MOUNT="loop0" df|awk '{print $1" is mounted on " $6 " !";}'|grep $MOUNT||echo -e "\033[1;31m$MOUNT is not mounted !"
Code: Select all
sudo losetup /dev/loop0 -j file.ext && echo OK || echo failed
I did it like this:L_V wrote:You then confirm the importance of first clarifying your goal.
Then, something like this ?Code: Select all
sudo losetup /dev/loop0 -j file.ext && echo OK || echo failed
Code: Select all
sudo losetup /dev/loop0 $1
if [[ $? -ne 0 ]]; then
echo "Error with $1!" >&2
exit 1
fi