Bash script - basic questions for simple script

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

Re: Bash script - basic questions for simple script

Postby PsySc0rpi0n » 2018-10-07 19:41

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: 37
Joined: 2012-10-24 13:54

Re: Bash script - evaluate if device is mounted

Postby PsySc0rpi0n » 2018-10-07 19:42

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: 37
Joined: 2012-10-24 13:54

Re: Bash script - basic questions for simple script

Postby PsySc0rpi0n » 2018-10-07 19:46

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.
User avatar
PsySc0rpi0n
 
Posts: 37
Joined: 2012-10-24 13:54

Re: Bash script - basic questions for simple script

Postby L_V » 2018-10-07 20:27

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

Re: Bash script - basic questions for simple script

Postby PsySc0rpi0n » 2018-10-09 19:46

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
User avatar
PsySc0rpi0n
 
Posts: 37
Joined: 2012-10-24 13:54

Previous

Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable