I have a variable I want to use in bash script. The user will pass an argument
to the script and I will store it in `arg_fql`. If the user does not pass the variable,
I still never set arg_fql, but I set another variable to a default. However, if the user
passes a value, `arg_fql` will be set to what the user specified
You did not specify the behaviour, if the user passes an argument of length zero. The general convention is to treat this as a "non-existing argument", so I will assume this in my solution, but of course there might be applications where we have to treat these two cases differently.
Assuming that the user passes the optional parameter to your script in $1, you could write:
Whether they produce the same results or not depends on which version of bash you are using.
On versions of bash less than or equal to at least version 3.2.57(1), test -v variable, [ -v variable ], and [[ -v variable ]] will give you an error because -r is not recognized as a valid operator.
In versions of bash where -r is recognized as an operator, the command sequence:
will produce the same final results as the command sequence:
but the first sequence will a run little bit slower and consume a little bit more system resources than the second sequence.
Note also that the second sequence is required to work in any shell that tries to conform to the POSIX standard's requirements for parameter expansions (and is, therefore, portable across many shells) while the first sequence uses an extension to the standards and, as far as I know, only works with some versions of ksh and some versions of bash.
Note also that even on shells that recognize -v as an operator, the output produced by:
is not equivalent to the output produced by:
in cases where arg_fql has been defined to be an empty string before running the above sequences. In this case, the first sequence will set fqi to an empty string while the second sequence will set it to the string 0.002 0.003 1 2.
Hello,
I would like to ask for help with csh script.
An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Hi all,
I want to dynamically set variables in a bash script. I made a naive attempt in a while loop that hopefully can clarify the idea.
n=0; echo "$lst" | while read p; do n=$(($n+1)); p"$n"="$p"; done
The error message is:
bash: p1=line1: command not found
bash: p2=line2: command... (8 Replies)
I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables.
I need to read this file which is an input to my script
Config.txt
file name, first path, second... (7 Replies)
Hi
This is a simple one but I got a lost in translation when doing.
What I want to do, given both variables in the example below, to get one value at the time from both variables, for example:
1:a
2:b
etc...
I need to get this in bash scripting
code:
varas="1 2 3 4"
varbs="a b c d"... (4 Replies)
Hello,
I've been struggling with this for some time but can't find a way to do it and I haven't found any other similar thread.
I'd like to get the 'fields' in a line from a file into variables in just one command.
The file contains data with the next structure:... (4 Replies)
I am using functions in a script and for some strange reason the EXPORT command doesnt seem to be making my variables global.
Anyone got any ideas?
I am using one function to pass some output top another using the pipe command, eg
Function 1 | Function 2
Function 2 reads the value... (3 Replies)
Hi all,
I have a shell script that sets up the environment for an application running on UNIX - ksh. This script is run using:
. ./script_name XX
where XX is a parameter. I want to run it from another shell script but when I do it I don't get the envornment variables set up and the prompt... (3 Replies)
i have a file .NAMEexport MY_NAME=JOE
when i do this at the command prompt #. .NAME
$echo MY_NAME
$JOEi created a script called Run.sh . .NAME At the command prompt i did #sh Run.sh
#echo $MY_NAMEit returns nothing. What have i missed out? (7 Replies)
Hi All,
I am setting bash as my working shell in my .profile file.
So I have written a line :
bash
as the list line in my .profile
I want to use alias as follows:
alias me='who am i'
When i log in, as expeced I enter the bash shell but alias doesn't work.
Is it because the alias is defined... (1 Reply)