LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-22-2006, 10:25 AM   #1
RAdams
Member
 
Registered: May 2006
Location: Cincinnati, Ohio
Distribution: Ubuntu
Posts: 256

Rep: Reputation: 30
Arrow Simple Bash Questions


These should be easy ones:
  1. Let's say I have a bash script containing the command:
    Code:
    gedit /foo/bar
    How can I make the script end after this command, but leave the gedit window open?
  2. If I have a script with parameters, how can I detect if no parameter has been added? For example, if the correct usage of my script is:
    [code]foobar parameter1 parameter2[code]
    How can I detect that the parameters were given by the user?
  3. On a releated note, how can I allow the script to assume a value for a parameter the user doesn't specify? For example, if I want paramter2 in the example above to be assumed as "baryum" if the user provides no input, how do I do that?
  4. What is the meaning of life?

That's all for now. Thank you very much.

Last edited by RAdams; 08-23-2006 at 12:54 AM.
 
Old 08-22-2006, 10:28 AM   #2
AAnarchYY
Member
 
Registered: Sep 2004
Location: Carlisle, MA
Distribution: Debian 8
Posts: 419

Rep: Reputation: 30
check out the article on shell scripting on ldp
http://www.tldp.org/LDP/abs/html/
 
Old 08-22-2006, 10:32 AM   #3
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 336Reputation: 336Reputation: 336Reputation: 336
1. Run the gedit as a background process, by ending the command with an '&'. This starts the gedit, but returns control to the script, which can then exit. For example:

Code:
gedit /foo/bar &
2. The way I prefer to do this is to check the variable against the null string:

Code:
if [ "$1" == "" ]
then
   echo "No parameters"
fi
3. You can provide defaults, by initializing a variable:

Code:
param2="baryum"
if [ "$2" != "" ]
then
   param2=$2
fi
4. There is no meaning to life. Enjoy it while you can - you're not getting out alive.
 
Old 08-22-2006, 10:39 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400Reputation: 2400
Hi,

AAnarchYY is correct, all the questions asked can be found there. Here's a bit more to get you started.

Quote:
Let's say I have a bash script containing the command:
Code:
gedit /foo/bar
How can I make the script end after this command, but leave the gedit window open?
Take a look at & ('start in background' operand)

Quote:
If I have a script with parameters, how can I detect if no parameter has been added? For example, if the correct usage of my script is:
[code]foobar parameter1 parameter2[code]
How can I detect that the parameters were given by the user?
getopts or $1, $2, $... are your friends, depending on complexity of parameters and/or options.

Quote:
On a releated note, how can I allow the script to assume a value for a parameter the user doesn't specify? For example, if I want paramter2 in the example above to be assumed as "baryum" if the user provides no input, how do I do that?
By using parameter expansion or by checking the parameter and setting it if a certain condition arises (if ... then ... elseif ... esle)

Quote:
What is the meaning of life?
Haven't you seen the movie yet, explains it all
 
Old 08-22-2006, 10:41 AM   #5
AAnarchYY
Member
 
Registered: Sep 2004
Location: Carlisle, MA
Distribution: Debian 8
Posts: 419

Rep: Reputation: 30
42, everyone knows that
 
Old 08-23-2006, 12:41 AM   #6
RAdams
Member
 
Registered: May 2006
Location: Cincinnati, Ohio
Distribution: Ubuntu
Posts: 256

Original Poster
Rep: Reputation: 30
Post

Thanks everyone. Piddling around, I managed to make a script to accelerate the process of making more scripts (Read: I'm lazy).

Essentially, the script copies information from a template file into a script name the user specifies, using whatever editor the user chooses (this parameter has a default).

Usage Example:
Code:
sudo newscript foobar nano
Would create a new script containing the information in a template to /usr/local/bin/foobar, set the permissions, and edit it using nano.

The template is meant to be a world-readable-writable file, though its permissions could be manually changed as one pleases.

There's other features too, but you'll see them below:

Code:
#! /bin/sh

EDITOR="gedit"
TEMPLATE="/usr/local/bin/bashtemplate"

#Check if run as root
if [ "$UID" -ne 0 ] ; then
	echo "Sorry, root privileges are required to run this script."
	exit 0
fi

#Check to make sure the user provided a name for the script
if [ "$1" == "" ]
then
	echo "Error: no scriptname specified! (Usage: newscript [scriptname] [editor])"
	exit -1
fi

#Check to see if an editor has been specified. If not, use default.

if [ "$2" != "" ]
then
	EDITOR=$2
fi


#Setup the Script
echo "Creating script..."
cp -i $TEMPLATE /usr/local/bin/$1
chmod 755 /usr/local/bin/$1
$EDITOR /usr/local/bin/$1
exit 1
Yes, it's not much, but it let me get my feet wet in scripting with passed parameters.

A question: when running this command under su instead of sudo, and using gedit for the editor, the terminal returns:
Code:
(gedit:10423): GnomeUI-WARNING **: While connecting to session manager:
Authentication Rejected, reason : None of the authentication protocols specified are supported and host-based authentication failed.
Why is this? What can I do to stop this error? It doesn't keep the script from working; I'm just curious about it.

One more question: is there a standard location in a Linux filesystem to store code templates?

Last edited by RAdams; 08-23-2006 at 12:51 AM.
 
Old 08-23-2006, 12:44 AM   #7
RAdams
Member
 
Registered: May 2006
Location: Cincinnati, Ohio
Distribution: Ubuntu
Posts: 256

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by AAnarchYY
42, everyone knows that
W,H,A,T,D,O,Y,O,U,G,E,T,W,H,E,N,Y,O,U,M,U,L,T,I,P,L,Y,S,I,X,B,Y,N,I,N,E
 
Old 08-23-2006, 06:44 AM   #8
AAnarchYY
Member
 
Registered: Sep 2004
Location: Carlisle, MA
Distribution: Debian 8
Posts: 419

Rep: Reputation: 30
Quote:
Originally Posted by RAdams
What can I do to stop this error? It doesn't keep the script from working; I'm just curious about it.
Not really sure what all that is but to stop it just put a &> /dev/null at the end of the command.
Code:
$EDITOR /usr/local/bin/$1 &> /dev/null
Also, why did you have the exit return a 1 upon success and a 0 upon failure? A successful command returns a 0, while an unsuccessful one returns a non-zero value.

code templates? sounds like something under /usr/share to me.
 
Old 08-23-2006, 09:48 AM   #9
RAdams
Member
 
Registered: May 2006
Location: Cincinnati, Ohio
Distribution: Ubuntu
Posts: 256

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by AAnarchYY
Not really sure what all that is but to stop it just put a &> /dev/null at the end of the command.
Code:
$EDITOR /usr/local/bin/$1 &> /dev/null
Also, why did you have the exit return a 1 upon success and a 0 upon failure? A successful command returns a 0, while an unsuccessful one returns a non-zero value.

code templates? sounds like something under /usr/share to me.
The exit codes are just a proprietary system I made up on the spot. I'll change it to standards, as I'm anal retentive about standards.

/usr/share? Makes sense to me.
 
  


Reply

Tags
bash, parameters


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
Simple Bash Script Help njdownes Programming 2 03-05-2005 07:35 AM
Simple Questions...Hopfully Simple Answers. caps_phisto Linux - General 3 12-21-2004 12:40 PM
Simple Bash Question sio Programming 6 11-22-2004 09:56 AM
simple bash q adam_boz Linux From Scratch 1 06-08-2003 11:22 AM
Simple BASH script fio Linux - General 5 01-18-2003 03:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:44 PM.

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
Open Source Consulting | Domain Registration