Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Bash script - basic questions for simple script

Programming languages, Coding, Executables, Package Creation, and Scripting.
Message
Author
User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Bash script - basic questions for simple script

#1 Post by PsySc0rpi0n »

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.

L_V
Posts: 1477
Joined: 2007-03-19 09:04
Been thanked: 11 times

Re: Bash script - evaluate if device is mounted

#2 Post by L_V »

Something like this ?

Code: Select all

MOUNT="loop0"
mount | grep $MOUNT && T="" || T=" not" ; echo "$MOUNT device is$T ready !"

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Bash script - evaluate if device is mounted

#3 Post by PsySc0rpi0n »

Could you please explain the code?

Thanks
Psy

peter_irich
Posts: 1403
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation
Been thanked: 11 times

Re: Bash script - evaluate if device is mounted

#4 Post by peter_irich »

Mounted devices are enumerated in /etc/mtab.

Peter.

L_V
Posts: 1477
Joined: 2007-03-19 09:04
Been thanked: 11 times

Re: Bash script - evaluate if device is mounted

#5 Post by L_V »

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"

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Bash script - evaluate if device is mounted

#6 Post by PsySc0rpi0n »

And no "if" statement is needed?

User avatar
debiman
Posts: 3063
Joined: 2013-03-12 07:18

Re: Bash script - evaluate if device is mounted

#7 Post by debiman »

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").

L_V
Posts: 1477
Joined: 2007-03-19 09:04
Been thanked: 11 times

Re: Bash script - evaluate if device is mounted

#8 Post by L_V »

Slightly different with same result:

Code: Select all

MOUNT="loop0"
T=" not";mount|grep $MOUNT && T=;echo "$MOUNT device is$T ready !"

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Bash script - evaluate if device is mounted

#9 Post by PsySc0rpi0n »

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

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Bash script - basic questions for simple script

#10 Post by arzgi »

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.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Bash script - basic questions for simple script

#11 Post by GarryRicketson »

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.

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Bash script - basic questions for simple script

#12 Post by arzgi »

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.

User avatar
debiman
Posts: 3063
Joined: 2013-03-12 07:18

Re: Bash script - evaluate if device is mounted

#13 Post by debiman »

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()

L_V
Posts: 1477
Joined: 2007-03-19 09:04
Been thanked: 11 times

Re: Bash script - basic questions for simple script

#14 Post by L_V »

@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 !"

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Bash script - basic questions for simple script

#15 Post by PsySc0rpi0n »

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: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Bash script - basic questions for simple script

#16 Post by PsySc0rpi0n »

arzgi wrote:
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.
I started with C when I started my (late) graduation! I know the basics of C.
I have also self-taught Python but had to quit due to school. Not enough time for both. I'm a day worker (full-time) and also a full-time parent. So, no much spare time left!

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Bash script - evaluate if device is mounted

#17 Post by PsySc0rpi0n »

debiman wrote:
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()
I tried the script with the '()?...
Didn't work either.

I think it is interpreted as a terminal command. Returned error is:

Code: Select all

command not found!

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Bash script - basic questions for simple script

#18 Post by PsySc0rpi0n »

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 !"
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
Posts: 1477
Joined: 2007-03-19 09:04
Been thanked: 11 times

Re: Bash script - basic questions for simple script

#19 Post by L_V »

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

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: Bash script - basic questions for simple script

#20 Post by PsySc0rpi0n »

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
I did it like this:

Code: Select all

sudo losetup /dev/loop0 $1
   if [[ $? -ne 0 ]]; then
      echo "Error with $1!" >&2
      exit 1
   fi

Post Reply