You are not logged in.

#1 2010-06-17 14:21:10

whacath
Member
Registered: 2009-05-26
Posts: 283

bash script failure

Why wont this script work?

#!/bin/bash
TUT='ls /dev/video*'
if [ "$TUT" == "/dev/video1" ]; then
sh /home/jonas/camcycle2.sh
else
sh /home/jonas/camcycle.sh
fi

What i want it to do is to list all video-devices in /dev/ and if there is a /dev/video1 it will run camcycle2.sh, if there is no video1, run camcycle.sh.

Last edited by whacath (2010-06-17 14:22:14)

Offline

#2 2010-06-17 14:23:02

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,092

Re: bash script failure

Because your logic is broken.

Also: you shouldn't be using ls

And you should learn to indent your code.

Last edited by Mr.Elendig (2010-06-17 14:23:16)


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

#3 2010-06-17 14:36:07

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: bash script failure

You want to execute camcycle.sh even if there is no video device?

#!/bin/bash
#
CAM=$(find /dev -name video1)
HDIR=/home/jonas
[ $CAM ] && sh $HDIR/camcycle2.sh || sh $HDIR/camcycle.sh

Last edited by demian (2010-06-17 14:42:10)


no place like /home
github

Offline

#4 2010-06-17 15:01:39

quigybo
Member
Registered: 2009-01-15
Posts: 223

Re: bash script failure

[ -e /dev/video1 ] && $HOME/camcycle2.sh || $HOME/camcycle.sh

Since /dev/video1 is a special file you could also use a different flag than -e, check the bash manpage (around l.2105) for more details.

EDIT: For the record the if/then construct you used is fine (with indenting or course), but since this is so simple a nice one-liner suffices.

Last edited by quigybo (2010-06-17 15:06:22)

Offline

#5 2010-06-17 15:05:59

whacath
Member
Registered: 2009-05-26
Posts: 283

Re: bash script failure

@Mr.Elending: Yay for your extreme helpfulness. Instead of telling me what i should do or not do, explain instead! tongue

@demian: No my tought is that if there is no video1 and there is a video0, camcycle.sh should kick in. Thanks for your example, i will have a look at it. smile

Offline

#6 2010-06-17 15:18:37

quigybo
Member
Registered: 2009-01-15
Posts: 223

Re: bash script failure

I just realised that this was posted in Community Contributions, you should have posted in Newbie Corner (thus avoiding responses such as Mr.Elendig's).

Back on topic: you can solve it with an if/elif chain and the -e test.

Offline

#7 2010-06-17 15:21:07

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: bash script failure

Then you should use the if construct:

if [ -e /dev/video1 ];then
    $HOME/camcycle2.sh
elif [ -e /dev/video0 ];then
    $HOME/camcycle.sh
fi


no place like /home
github

Offline

#8 2010-06-17 16:56:44

Hiato
Member
Registered: 2009-01-21
Posts: 76

Re: bash script failure

Could someone please move this? Why is this in community contributions in the first place, whacath? (There is a tiny chance I am mistaken as to the purpose of this section, however)

Offline

#9 2010-06-17 17:01:15

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: bash script failure

Moved.

Offline

#10 2010-06-17 18:01:51

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: bash script failure

FYI, you use TUT='ls /dev/video*' which would not capture the output of ls. You should have done TUT="$(ls /dev/video*)" to capture the output. That would have still been wrong if there were more than one video device in /dev. The correct logic to use is given by demian in post #7.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#11 2010-06-17 21:15:59

whacath
Member
Registered: 2009-05-26
Posts: 283

Re: bash script failure

Thanks guys!

So do you have any links or something to a good bash guide? becouse i really need to learn it the right way. tongue

Offline

#12 2010-06-17 21:23:41

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: bash script failure

There are a good set of documents on TLDP. Here are a choice few: Advanced Bash-Scripting Guide, Bash Guide for Beginners, BASH Programming - Introduction HOW-TO.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#13 2010-06-17 22:58:37

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: bash script failure

The TLDP guides are pretty dated, and fairly rough on a beginner if you're not aware of what you need to filter out (and you do need to filter some out). I highly recommend Greg's BashGuide. It's well written, simple, and provides excellent examples. The rest of the Wiki on the site is an absolutely phenomenal resource.

Offline

Board footer

Powered by FluxBB