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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I play in Pocky version with some scripts and I get problem with a very simple structure of "if" to compare a numeric value and execute something and I can't figure out why doesn't want execute correct.
The diag variable takes values 0 or 1.
I have a "system.ini" file where is memorized this numeric value:
Code:
[version]
diag=1
a library file "common.sh" where are defined common variables:
There probably shouldn't be quotes around a variable if it is to be treated like an integer.
You could try adding the following after the "source" line:
Code:
set -xv
That will show you what is actually in the variable and the exact line being parsed by the script.
However, there are perl and python modules for parsing INI files. If you're getting a more complex INI file to deal with, one of those might be worth looking at instead.
You might have some whitespace characters in your `system.ini` file (space, tab, carriage return etc)
Perhaps you should start it again from a working version (see the attached file).
The reading value is correct but if condition for numeric value doesn't execute.
What I miss?
That's a very good question, because I just replicated your setup by pasting the code/data into the three files involved (common.sh, system.ini, and the main batch file), and it seems to work just fine.
Please run the main file with "bash -x <filename>" and post the results.
❯ cat common.sh
ModDIAG=$(awk -F "=" '/diag/ {print $2}' system.ini)
❯ cat test.sh
#!/usr/bin/env bash
source common.sh
echo "ModDiag=$ModDIAG"
if [[ "$ModDIAG" -eq 1 ]]
then
echo "This is a test"
fi
echo "End test"
❯ bash test.sh
ModDiag=1
This is a test
End test
When I tried it, it's working as expected?
I tried adding some trailing whitespace to system.ini and it didn't change anything?
for things like this, i find the python language easier to achieve success. it is worth learning. all the major distributions of Linux include python version 3.
Why not - since there is no specification I guess you get to make up the rules. This version limits line length, reads a file or stdin, accepts a key or section:key as argument and prints its findings to stdout. Probably not very useful and I have no intention of doing anything farther with it but it did use up some, otherwise valuable, time. :P
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_BUFFER_SIZE 1025
void usage (char* app);
int get_section_key (char* arg, char** section, char** key);
int check_for_key (char* line, char* section);
int check_for_section (char* line);
int main (int argc, char** argv) {
FILE* in;
if (argc < 2) {
usage (argv[0]);
exit (EXIT_FAILURE);
}
if (argc == 3) {
in = fopen (argv[2], "r");
if (in == NULL) {
perror ("fopen");
fprintf (stderr, "Unable to open %s for reading\n", argv[2]);
usage (argv[0]);
exit (EXIT_FAILURE);
}
} else {
in = stdin;
}
char* section = NULL;
char* key = NULL;
int has_section = get_section_key (argv[1], §ion, &key);
int c = 0;
char current_line[LINE_BUFFER_SIZE];
char current_section[LINE_BUFFER_SIZE] = { 0 };
int current_count = 0;
int section_found = 0;
while ((c = fgetc (in)) != EOF) {
if (c == '\n') {
if (current_count == 0) {
continue;
}
if (current_count == LINE_BUFFER_SIZE) {
fprintf (stderr, "Maximum line length of %d exceeded\n", LINE_BUFFER_SIZE);
exit (EXIT_FAILURE);
}
current_line[current_count] = '\0';
current_count = 0;
section_found = check_for_section (current_line);
if (section_found) {
strncpy (current_section, current_line + 1, strlen (current_line) - 2);
} else {
if (!has_section || (has_section && (strcmp (section, current_section) == 0))) {
if (check_for_key (current_line, key)) {
printf ("%s:%s\n", current_section, current_line);
}
}
}
} else {
current_line[current_count] = (char)c;
++current_count;
}
}
if (argc == 3) {
fclose (in);
}
if (!feof (in) && ferror (in)) {
perror ("fgetc");
exit (EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
void usage (char* app) {
printf ("usage: %s [section:]key [filename]\n", app);
}
int get_section_key (char* arg, char** section, char** key) {
char* parg = arg;
int has_section = 0;
while (*parg != '\0') {
if (*parg == ':') {
has_section = 1;
*parg = '\0';
++parg;
*key = parg;
*section = arg;
break;
}
++parg;
}
if (!has_section) {
*key = arg;
}
return has_section;
}
int check_for_section (char* line) {
int found = 0;
int len = strlen (line);
if (line[0] == '[' && line[len - 1] == ']') {
found = 1;
}
return found;
}
int check_for_key (char* line, char* key) {
char* pline = line;
char* pkey = key;
int found = 0;
while (*pline != '\0') {
if (*pline == '=') {
found = 1;
break;
}
if (*pkey == '\0') {
break;
}
if (*pline != *pkey) {
break;
}
++pline;
++pkey;
}
return found;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.