You are not logged in.

#1 2013-01-25 23:01:38

nuc
Member
Registered: 2012-04-26
Posts: 115

[DONE] Bash scripting. A few questions

Hi,
I've created my own custom sleep script as it is described here: https://wiki.archlinux.org/index.php/Sy … stem-sleep and it's already working correctly.
However I've got a few questions concerning bash scripting since I've never did it before.
Here the example from the wiki:

#!/bin/sh
case $1/$2 in
  pre/*)
    echo "Going to $2..."
    ;;
  post/*)
    echo "Waking up from $2..."
    ;;
esac

Now what do I need the /*) for in the cases pre and post? What does it do? And why isn't there any *) in the end which would exit the script if none of the above is true?

Secondly, why does it say case $1/$2 in, but not case $1 in, what is the $2 needed for? E.g. in this script only $1 is used: https://wiki.archlinux.org/index.php/OS … ibernation

And third question is, if I do really need the double quotes around $1? Does it make any difference?


Sorry for my incompetence but I'm not a dev and as I already said I've never did any bash scripting...

Thanks in advance

best regards
nuc

Last edited by nuc (2013-02-07 18:10:56)

Offline

#2 2013-01-25 23:14:57

digirium
Member
Registered: 2012-11-15
Posts: 51

Re: [DONE] Bash scripting. A few questions

Well, the case could just use $1 only as the first argument to the script is either pre or post. That would make the two cases within the case-switch just pre) and post) without slashes or asterix. What double quotes did you mean? Only see them in the echos. There is no default in the case-switch as the inputs are limited.

Last edited by digirium (2013-01-25 23:18:02)

Offline

#3 2013-01-25 23:17:06

nuc
Member
Registered: 2012-04-26
Posts: 115

Re: [DONE] Bash scripting. A few questions

thx for your answer. I meant the double quotes in this script: https://wiki.archlinux.org/index.php/OS … ibernation

Last edited by nuc (2013-01-25 23:17:23)

Offline

#4 2013-01-25 23:20:11

digirium
Member
Registered: 2012-11-15
Posts: 51

Re: [DONE] Bash scripting. A few questions

The case command might get confused if the argument between case and in is more than one word. That is why it is a good idea if the argument might contain more than one word to surround it with double quotes. In that OSS script the double quotes are strictly not necessary judging from the cases within the case-switch. But the OSS script uses the double quotes in case the parameters on the command-line were given in error.

Last edited by digirium (2013-01-25 23:21:54)

Offline

#5 2013-01-25 23:27:49

nuc
Member
Registered: 2012-04-26
Posts: 115

Re: [DONE] Bash scripting. A few questions

ok, here's my actual code:

#!/bin/sh
suspend_osssound()
{
    /usr/lib/oss/scripts/killprocs.sh
    /usr/sbin/soundoff
}
 
resume_osssound()
{
    /usr/sbin/soundon
}
 
case "$1" in
    pre)
        suspend_osssound
 	;;
    post)
 	resume_osssound
 	;;
    *) exit $NA
 	;;
esac

It is the same as the one in the OSS wiki entry, but slightly configured to work under logind and not pm-utils. Of course the new location is /usr/lib/systemd/system-sleep/.
I'm about to update the wiki with my code, so I'd like to know if there aren't any major mistakes. I tested it and it works but I want to get sure smile


PS: Do people actually still use pm-utils for suspending or can I safely remove the previous script from the wiki?

EDIT: Added double quotes

Last edited by nuc (2013-01-25 23:42:02)

Offline

#6 2013-01-25 23:28:12

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,388
Website

Re: [DONE] Bash scripting. A few questions

Quotes are always a good idea.  If you don't use quotes in the case statement, someone could easily break your script by quoting command line parameters.  For example with `script "one two" three` the first parameter actually contains the "one two",  The case statement would expand that to `case one two in` and there would be an error.  But if you quote the $1 then it would expand to `case "one two" in` and it would work as intended.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#7 2013-01-26 19:08:47

nuc
Member
Registered: 2012-04-26
Posts: 115

Re: [DONE] Bash scripting. A few questions

Thanks for the info Trilby! Did you read my post above? They've been written in the same time, so might be you've overlooked it.

Offline

#8 2013-01-26 19:44:30

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

Re: [DONE] Bash scripting. A few questions

Trilby wrote:

Quotes are always a good idea.  If you don't use quotes in the case statement, someone could easily break your script by quoting command line parameters.  For example with `script "one two" three` the first parameter actually contains the "one two",  The case statement would expand that to `case one two in` and there would be an error.  But if you quote the $1 then it would expand to `case "one two" in` and it would work as intended.

Incorrect. The argument of a case statement is not subject to any form of expansion that might break this. You never need to quote it unless it's a string literal or you intentionally pass something which contains space.

There's no glob expansion or syntax error here...

v='foo bar*'
case $v in
  ...
esac

Offline

#9 2013-01-26 19:55:17

nuc
Member
Registered: 2012-04-26
Posts: 115

Re: [DONE] Bash scripting. A few questions

Thanks for clarification.
So I guess this statement isn't correct, too:

digirium wrote:

[...] But the OSS script uses the double quotes in case the parameters on the command-line were given in error.

Offline

#10 2013-01-26 20:30:46

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,388
Website

Re: [DONE] Bash scripting. A few questions

Oops, learn something new every day - sorry for the misinformation.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#11 2013-01-27 01:26:15

nuc
Member
Registered: 2012-04-26
Posts: 115

Re: [DONE] Bash scripting. A few questions

I applied my script to the wiki today, replacing the old one: https://wiki.archlinux.org/index.php/OS … ibernation

Thanks again for your help!

Offline

Board footer

Powered by FluxBB