Shell script variables
Hi
I have a script which might have some variables in it depending on what the user has chosen. The variables will be in in format $RemoveN, N being an even number (like $Remove2, $Remove4, $Remove 6 and so forth). they will equal Remove, and the N represents a line number in a file. I need to delete this line when N=Remove. So if $Remove4 is null, do nothing, but if $Remove6 eq Remove then delete line 6 from file /var/user/rules. Any simple way to do this? I could always go for the easy option and do a long list where he checks every variable at a time but I thought there must be a easier way to do it. |
Code:
: ${Remove66=} |
Sorry I am fairley new to this. Could you give me a quick description to as what it does and how I can implement it?
|
Well, you just said you had the variable names and didn't talk about their values, so I had to make some (empty) variables to make it work as an example:
Code:
: ${Remove66=} Code:
for number in ${!Remove*}; do |
Thank you for your reply. I gave that an try but it just spat out a syntax error, bad substitution.
|
I gave that an try but it just spat out a syntax error, bad substitution.
Please dont *talk* about errors but *post* the exact one. |
Sorry but that is the exact one, it says "Syntax Error: Bad substitution". My /bin/sh doesnt support substitution that is why. Is there anyother way to achive what I am looking to do?
Regards Fredrik |
Sorry but that is the exact one, it says "Syntax Error: Bad substitution". My /bin/sh doesnt support substitution that is why. Is there anyother way to achive what I am looking to do?
Are you saying you're using Bourne and not Bash? Sure there will be another way. Could you post a download location for the script? |
I havent got a script yet... It is going to be a cgi script which gets directed to when the user presses a button (or perhaps if a user developes an application to delete several lines at once). This is what it looks like so far,
Code:
#!/bin/sh Code:
#!/bin/sh So what query_strin2sh does is it will spit out eveything sent in the POST to its own variable, so if textfield=Test%20123 then there will be a variable called $PARAM_textfield with the value of "Test 123". Now this script I am creating now is for the remove button. There can be as many Remove buttons as the user wants (depends on how many lines there is in a config file). They are all called by the following serie, Remove2, Remove4, Remove6, Remove8, Remove10 and so forth. They will either not have a value att all (button not pressed) or string "Remove" (button is called Remove, so the label is sent as the value). Now if we have the following case scenario PARAM_Remove2= PARAM_Remove4= PARAM_Remove6= PARAM_Remove8= PARAM_Remove10=Remove PARAM_Remove12= PARAM_Remove14= The user has in this case pressed the fifth button which represents line 10. The script is then called and the text string Remove is put into $PARAM_Remove10. So now I need to delete lines 10 and 11 from file /var/temp How do I do this? To summerise, I want to do the following but with a better solution Code:
if [ $PARAM_Remove2 = Remove ]: then Hope this clears things up. Thanks |
Here's an idea. It's tested except for the sed part. It does use *Bash* though. Converting it to Bourne could be done (by you, not me). I wedged in some minimal checks and added TODO list items if you want to expand this. You should change FILE_NAME and FILE_TEMP_NAME locations and fill FILE_NAME with meaningful content for testing. If your webserver does not allow you to do file ops in the location you specify (good) then set up a Sudo Cmnd_Alias with NOPASSWD for it. No servicable user parts inside. Usability not implied nor functionality guaranteed. Store cold and dry. Do not drop. Keep away from children. Shell scripting addiction hazard warning.
YMMV(VM), as always ;-p Code:
#!/bin/bash - |
Hmm. Come to think of it, you can slash the whole iteration thing if you can make queryAction return all and only numbers. Then you could make removeRoutine do sed '${number}d;${number}d;${number}d;' file.
|
Sorry you will have to excuse me, I am fairly new to this. Especially bash scripting, never written in bash, always sticked to bourne. I cant use any "${XXXX}" (If I understand that correctly what it does is check that the variable exists and that it is not null (unless it has semicolons in it).). Ill give it a play later today.
Edit: Got it to work! I had a hard time with getting your script to work though but your script pointed me in the right direction! Thank you very much! Switching to checkboxes and adding, Code:
if [ $var = LINENO ]; then Now I can easily do a for script to delete one line at a time. They are also reversed so that it starts the deletion at the back. Thank you so much for your help! |
Especially bash scripting, never written in bash, always sticked to bourne.
To me it seems a rather rare and valueable quality... I can't see why you need to go through all sort of hoops with the querystring2sh? If you do enough tests on QUERY_STRING to make sure it's valid and sanitise (sed) unwanted parts you almost have your sed command. So I now have the variable $PARAM_LINENO2 which contains the lines with a . as a delimiter, so it looks like this, "10.6.4.2." If you can make $PARAM_LINENO2 read "10d;6d;4d;2d;" instead you already have your sed command and you can ditch the for loop. Anyway. Good to see you got it working. |
Never thought about that, might try that. Clean up some code. Thank you very much!
|
All times are GMT -5. The time now is 07:09 PM. |