Bash script - basic questions for simple script

Need help with C, C++, perl, python, etc?

Bash script - basic questions for simple script

Postby PsySc0rpi0n » 2018-10-05 14:58

How can I check if, loop0 for instance, is mounted and ready to be used?
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
Last edited by PsySc0rpi0n on 2018-10-06 22:47, edited 1 time in total.
User avatar
PsySc0rpi0n
 
Posts: 37
Joined: 2012-10-24 13:54

Re: Bash script - evaluate if device is mounted

Postby L_V » 2018-10-05 15:11

Something like this ?
Code: Select all
MOUNT="loop0"
mount | grep $MOUNT && T="" || T=" not" ; echo "$MOUNT device is$T ready !"
L_V
 
Posts: 799
Joined: 2007-03-19 09:04

Re: Bash script - evaluate if device is mounted

Postby PsySc0rpi0n » 2018-10-05 15:42

Could you please explain the code?

Thanks
Psy
User avatar
PsySc0rpi0n
 
Posts: 37
Joined: 2012-10-24 13:54

Re: Bash script - evaluate if device is mounted

Postby peter_irich » 2018-10-05 17:10

Mounted devices are enumerated in /etc/mtab.

Peter.
peter_irich
 
Posts: 1234
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation

Re: Bash script - evaluate if device is mounted

Postby L_V » 2018-10-05 18:12

PsySc0rpi0n wrote:Could you please explain the code ?

Not so much to explain, but in short:
- `mount` lists what is currently mounted;
- looking for loop0 with grep
- if loop0 is found, then "T" is empty; if not, then T=" not"
L_V
 
Posts: 799
Joined: 2007-03-19 09:04

Re: Bash script - evaluate if device is mounted

Postby PsySc0rpi0n » 2018-10-05 19:05

And no "if" statement is needed?
User avatar
PsySc0rpi0n
 
Posts: 37
Joined: 2012-10-24 13:54

Re: Bash script - evaluate if device is mounted

Postby debiman » 2018-10-05 19:12

PsySc0rpi0n wrote:And no "if" statement is needed?

the command1 && command2 || command3 kindof takes care of that:
if command1 returns true, execute command2 (T=''), otherwise execute command3 (T=" not").
User avatar
debiman
 
Posts: 3064
Joined: 2013-03-12 07:18

Re: Bash script - evaluate if device is mounted

Postby L_V » 2018-10-05 19:27

Slightly different with same result:
Code: Select all
MOUNT="loop0"
T=" not";mount|grep $MOUNT && T=;echo "$MOUNT device is$T ready !"
L_V
 
Posts: 799
Joined: 2007-03-19 09:04

Re: Bash script - evaluate if device is mounted

Postby PsySc0rpi0n » 2018-10-06 22:47

Ok, I made it without needing to use this method!
Anyway, I have other questions regarding to a small script I'm trying to create! And I'm completely new to bash scripting so maybe I change the title of this thread and ask here any other questions I need to ask!

Why is this "case esac" returning syntax errors at the first ";;":

Code: Select all
case $opt in
   a|A)
      make_available()
      ;;
   u|U)
      make_unavailable()
      ;;
   x|X)
      exit 1
      ;;
   *)
      echo "Unknown error!" >&2
      ;;
esac


Already tried with double quotes for patterns, but no good either:

Code: Select all
case $opt in
   "a"|"A")
      make_available()
      ;;
   "u"|"U")
      make_unavailable()
      ;;
   "x"|"X")
      exit 1
      ;;
   *)
      echo "Unknown error!" >&2
      ;;
esac
User avatar
PsySc0rpi0n
 
Posts: 37
Joined: 2012-10-24 13:54

Re: Bash script - basic questions for simple script

Postby arzgi » 2018-10-07 07:08

It's fine you want to learn bash. There is abs-guide package in the repo, Advanced Bash scripting guide. Also there are many tutorials in net.
arzgi
 
Posts: 389
Joined: 2008-02-21 17:03
Location: Finland

Re: Bash script - basic questions for simple script

Postby GarryRicketson » 2018-10-07 07:43

Why is this "case esac" returning syntax errors at the first ";;":

Maybe,first hit: http://wiki.bash-hackers.org/syntax/ccmd/case
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.

====
And I'm completely new to bash scripting so maybe----snip---

So maybe start out with some simpler scripts, and learn more, before trying
more advanced things. It is hard to explain trigonometry to a math student that has not yet learned to add, subtract ,divide, multiply, etc.....

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.


Also, there are better alternatives then "bash" , some people believe that bash stands for "broken again shell" with despicable security record.
User avatar
GarryRicketson
 
Posts: 5192
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Bash script - basic questions for simple script

Postby arzgi » 2018-10-07 08:21

GarryRicketson wrote:

Also, there are better alternatives then "bash" , some people believe that bash stands for "broken again shell" with despicable security record.


That's subjective. Bash is the most popular, and can be used to most scripting needs.

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.
arzgi
 
Posts: 389
Joined: 2008-02-21 17:03
Location: Finland

Re: Bash script - evaluate if device is mounted

Postby debiman » 2018-10-07 08:31

PsySc0rpi0n wrote:Why is this "case esac" returning syntax errors at the first ";;":

you really need to SHOW us!
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()
User avatar
debiman
 
Posts: 3064
Joined: 2013-03-12 07:18

Re: Bash script - basic questions for simple script

Postby L_V » 2018-10-07 17:33

@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 !"
L_V
 
Posts: 799
Joined: 2007-03-19 09:04

Re: Bash script - basic questions for simple script

Postby PsySc0rpi0n » 2018-10-07 19:39

GarryRicketson wrote:Why is this "case esac" returning syntax errors at the first ";;":

Maybe,first hit: http://wiki.bash-hackers.org/syntax/ccmd/case
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.



I can't see anything in that link talking about indentation!

GarryRicketson wrote:====
And I'm completely new to bash scripting so maybe----snip---

So maybe start out with some simpler scripts, and learn more, before trying
more advanced things. It is hard to explain trigonometry to a math student that has not yet learned to add, subtract ,divide, multiply, etc.....


It's the most basic script I get get other than ---echo "Hello World!".

GarryRicketson wrote:
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.


Also, there are better alternatives then "bash" , some people believe that bash stands for "broken again shell" with despicable security record.


Ok, thanks for replying!
User avatar
PsySc0rpi0n
 
Posts: 37
Joined: 2012-10-24 13:54

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable