Combining a declared variable with a temporary variable
Hi folks!
Kind of a noob question... from an OLD AIX/HPUX Admin.
I am writing a script to ease use of a command; an extended aliasing if you will. What I want to do is set several variables (OPT1, OPT2, etc) with command arguments, such as --help, --list-all, etc. Later in the script, I have a menu that queries for a number option and the selects the appropriate declared variable.
What i need to do is pass $COMMAND $OPT"$NUMBER". How is this quoted to make it happen? See my code below.
Quoting won't make it possible. What you are trying to do is not allowed by the shell command language. It could be done using the eval command, but there can be undesired side-effects which lead most people to avoiding its use unless you really know what you're doing and knowing every possible setting for all variables that are being processed by eval.
Since you're using bash as your shell, you might be better off creating an array of options and using the selection value read into the PICK variable as a subscript into that array to choose the option to be applied.
This User Gave Thanks to Don Cragun For This Post:
I was starting to suspect that was the case. I did some quick research after posting and I didn't see where what I wanted to do was even referenced, and surely I can't be the only person that ever wanted to do something like that.
Damn, but I'm rusty. It's been nearly 20 years since I wrote a shell script.... and those were all in KSH.
For that reason, I will rule out `eval`.
I did come up with a functional, if inelegant solution; it works as I want even if it IS ugly as hell.
Thanks for the reply.
--- Post updated at 11:50 AM ---
RudiC,
It does, and I'll read up on using it. This won't be the last time I write this type of script. The CLI commands keep getting longer and longer. Running Centos 7 as a home media/file server.
See if this quite incomplete sketch can be used as a starting point for your future solution:
It uses the select statement incl. the PS3 variable for building the menu, and "variable indirection" to build the command, here used with echo for simplicity. Supplying arguments as command line arguments is a better choice than making them a part of the command, btw.
A case statement leans itself towards being used in the loop, as is the use of array as alluded to by Don Cragun.
I have a below syntax its working fine...
var12=$(ps -ef | grep apache | awk '{print $2,$4}')
Im getting expected output as below:
printf "%b\n" "${VAR12}"
dell 123
dell 456
dell 457
Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Hi All,
I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Hi all,
Hereby wish to have your advise for below:
Main concept is
I intend to get current directory of my script file.
This script file will be copied to /etc/init.d.
A string in this copy will be replaced with current directory value.
Below is original script file:
... (6 Replies)
Hi guys, kinda new to unix/linux, could you please help me figure this out.
i need to write in a file using printf.
printf "%-20s %-40s %-20s\n" $a $b $c >> out.txt
but a, b and c are declared in a header file:
a='I am a dog'
b='I am a cat'
c='I am a fish'
i want the file to look like... (1 Reply)
Hello, I am a new joiner to the forum, and have what i hope is a simple question, however I can't seem to find the answer so maybe it is not available within bash scripting.
I intend to use the below script to archive files from multiple directories at once by using a loop, and a variable (n)... (10 Replies)
can anyone please help me with this:
i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure...
... (4 Replies)
Hi all,
I have a variable say var1 (output from somewhere, which I can't change)which store something like this:
echo $var1
name=fred
age=25
address="123 abc"
password=pass1234
how can I make the variable $name, $age, $address and $password contain the info?
I mean do this in a... (1 Reply)
Hi Friends,
I am trying to run a sql query from shell script as below but I get "Bind variable "1" not declared" error.
1.sh shell script has following:
sDb="abc/xyz@aaa"
a="1.sql"
sqlplus -s $sDb @$a $1
1.sql file has following:
spool Result.tmp append
select cust_name from orders... (1 Reply)
I want to instert Category:XXXXX into the 2. line
something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong:
sed "2i\\${n}Category:$cat\n"
Sample:
Titel Blahh Blahh abllk sdhsd sjdhf
Blahh Blah Blahh
Blahh
Should look like... (2 Replies)
Hi, I have two files.
File1:
File1 contains two fixed width columns ID of 15 characters length and Name is of 100 characters length.
ID Name
1-43<<11 spaces>>Swapna<<94 spaces>>
1-234<<10 spaces>>Mani<<96 spaces>>
1-3456<<9 spaces>>Kapil<<95 spaces>>
File2:
... (4 Replies)