ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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?
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?
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?
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,501
Rep:
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.
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)
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.