LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-07-2012, 01:22 AM   #16
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957

To start with, please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.


You're still trying to put a command inside a single variable. That's not the correct way to do it.

This is the kind of thing you should set up as a function. Remember, variables (including arrays) store data, functions store code (i.e. commands).

First define the function, usually somewhere near the top of the script, but always before you need it.

Code:
RunAfter() {
	# Simply uses curl to download and print all urls passed to it.
	curl "$@"
}
Since curl can operate on multiple urls at once, you really only need a single instance (hint, learn how to make effective use of the features of the commands you're calling).

Then at the appropriate place in the script, use it just like any other command.

Code:
RunAfter "http://myserver.tld/generate.php" "http://myserver.tld/translate.php"
http://mywiki.wooledge.org/BashGuide...ands#Functions


Although actually, the desired operation is so simple in this case why bother with a function at all?, why not just use curl directly? functions are really there for when you need to create a complex command block that will be used repeatedly.


BTW, if your server name is always the same, consider storing it in a separate variable too. The general principle is to avoid hard-coding of data and duplication of code wherever possible.

Code:
servername='http://myserver.tld'

curl "$servername/"{generate,translate}.php
Notice here how I concatenated the quoted variable string with the unquoted brace expansion.
 
Old 09-07-2012, 01:49 AM   #17
Nagy Szilveszter
LQ Newbie
 
Registered: Sep 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
I think the one i gave you was just an unlucky example and lead you the wrong direction. This is not just about curl. At the project i quoted here it was only 2 curl to be run, BUT

the RUNAFTER (variable - or better now array) should be able to correctly contain any kind of BASH command i need.


Let's go with this example (i'll still write it the wrong way since i'm not yet very familiar with arrays):

Quote:
RUNAFTER = 'curl $servername/generate.php ; curl $servername/translate.php ; chown apache:hosting $hostedfolder ; chmod 0770 $hostedfolder ; /scripts/sendnotifications.sh "SMS, YM" ; format c: '
This is not a real-world example, it's just to show you the idea of RUNAFTER usage that it should be able to handle any kind of BASH commands.

Note that there are commands that have 2 arguments (chown, chmod), and there are commands that have "" in arguments (my sendnotifications script that sends SMS and Yahoo Messenger messages to me)

The main challenge i see is the variable number of arguments and quotes.
BTW, if i define this RUNAFTER command as an array i think both of my problems will disappear.
So i think my only need is to be able to define / and handle such an array correctly.

Last edited by Nagy Szilveszter; 09-07-2012 at 01:55 AM. Reason: Minor fixes
 
Old 09-07-2012, 09:54 AM   #18
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I say you're still getting the whole concept wrong. Try forgetting completely about containing commands in variables/arrays, and start thinking in terms of functions.

If you want to be able to choose and execute from a preset list of commands, put them all in a function, and set up a way to select the one(s) you want from that list. Something like this, maybe:

Code:
RunAfter(){
	# each argument passed is the number of a command to run
	local n
	for n in "$@"; do
		case $n in
			1) curl "$servername/generate.php" ;;
			2) curl "$servername/translate.php" ;;
			3) chown apache:hosting "$hostedfolder" ;;
			4) chmod 0770 "$hostedfolder" ;;
			5) /scripts/sendnotifications.sh "SMS, YM" ;;
			6) format c:  ;;
		esac
	done
}
And then to run the chown and chmod commands together:

Code:
RunAfter 3 4
You might want to change the numbers to name strings, if you want to keep the code more comprehensible.

Things could get more complex if you have to also pass arguments to the commands directly, but it can be done.
 
1 members found this post helpful.
Old 09-07-2012, 10:19 AM   #19
Nagy Szilveszter
LQ Newbie
 
Registered: Sep 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
This way it's acceptable.

And i learnt now how to define and use functions in BASH. Rep goes to you.

Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Yet Another Bash Quotes Within Quotes Issue tboyer Linux - Software 17 11-03-2012 12:17 PM
translate value from single quotes to double quotes venkateshrupineni Linux - Newbie 2 06-14-2012 04:03 PM
Problems with quotes and double quotes Andruha Slackware 6 01-02-2010 05:44 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 12:41 PM
PostNuke install, magic quotes problem HippieCat Linux - Software 0 02-21-2005 05:06 PM


All times are GMT -5. The time now is 01:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration