Bash Script um DNS umzustellen?

Habe einmal ein Bash Script und eine .Desktop Verknüpfungen auf meinem Arch KDE und wollte bei Doppelklick auf die Verknüpfungen zb Updaten oder DNS auf 8.8.8.8 oderr Automatisch Umstellen.

(Ich kann auch via KDE Connect das ganze Starten und ohne EIngabe abschließen mit sudo pacman -Syu --noconfirm && yay -Syu --noconfirm)

Aber bei toggle_dns funktioniert das leider nicht mit dem gelieferten Code von ChatGPT (siehe Unten) und ich kann garkein Bash, nur "normale" programmiersprachen wie JavaScript oder PHP. Mit Python würde ich es vielleicht hinkriegen aber das ist doch viel aufwendiger jetzt die IDE erneut zu installieren als mit Bash.

Wenn es das gibt wollte ich eine KDE eigene Benachrichtigung in welchen Modus es jetzt gewechselt ist zb : DNS auf Automatisch umgestellt oder DNS auf 8.8.8.8 Umgestellt statt ein Terminal was ich dann noch per hand schließen muss.
Kann mir jemand weiterhelfen den Code zu reparieren ?

toggle_dns.sh :

#!/bin/bash

# Datei und Schnittstelle für die DNS-Konfiguration
RESOLV_CONF="/etc/resolv.conf"
INTERFACE="Netzwerkschnittstelle_von_euch"

# Funktion zum Setzen des DNS-Servers auf 8.8.8.8
set_dns() {
  echo -e "nameserver 8.8.8.8\noptions edns0" | sudo tee $RESOLV_CONF > /dev/null
  sudo systemctl restart NetworkManager
  notify "DNS wurde auf 8.8.8.8 gesetzt."
}

# Funktion zum Zurücksetzen des DNS-Servers auf automatisch
reset_dns() {
  sudo rm -f $RESOLV_CONF
  sudo systemctl restart NetworkManager
  notify "DNS wurde auf automatisch gesetzt."
}

# Funktion zum Senden einer KDE-Benachrichtigung
notify() {
  message=$1
  kdialog --passivepopup "$message" 5
}

# Überprüfen, ob der DNS-Server bereits auf 8.8.8.8 gesetzt ist
if grep -q "8.8.8.8" $RESOLV_CONF; then
  echo "DNS ist derzeit auf 8.8.8.8 gesetzt. Umschalten auf automatisch."
  reset_dns
else
  echo "DNS ist derzeit automatisch konfiguriert. Umschalten auf 8.8.8.8."
  set_dns
fi

Bild zum Beitrag
PC, Server, Computer, Internet, Windows, WLAN, Technik, Linux, IT, programmieren, Java, JavaScript, Ubuntu, Cplusplus, Bash, C Sharp, DNS, Gnome, Informatik, KDE, Programmiersprache, Python, Script, Shell, Arch Linux, Rust, Debian, PowerShell, KDE Plasma, node
Arduino-Programmierung: Denkfehler?

Ich habe einen Testcode geschrieben, den ich im tinkercad-Simulator laufen lassen möchte.

Zwei Schalter sollen mit den Zuständen 000 bis 1111 vier Ausgänge schalten.

Dafür dieser Code:

const int input1 = 2; // Digitaleingang 1
const int input2 = 3; // Digitaleingang 2

const int output1 = 8; // Digitaler Ausgang 1
const int output2 = 9; // Digitaler Ausgang 2
const int output3 = 10; // Digitaler Ausgang 3
const int output4 = 11; // Digitaler Ausgang 4

void setup() {
  // Konfiguration der Pins als Eingänge und Ausgänge
  pinMode(input1, INPUT);
  pinMode(input2, INPUT);
  pinMode(output1, OUTPUT);
  pinMode(output2, OUTPUT);
  pinMode(output3, OUTPUT);
  pinMode(output4, OUTPUT);
}

void loop() {
  // Lesen des Zustands der Eingänge
  int input1Value = digitalRead(input1);
  int input2Value = digitalRead(input2);

  // Berechnen der Ausgangswerte
  int output1Value = (input1Value == 0) && (input2Value == 0);
  int output2Value = (input1Value == 0) && (input2Value == 1);
  int output3Value = (input1Value == 1) && (input2Value == 0);
  int output4Value = (input1Value == 1) && (input2Value == 1);

  // Schreiben der Ausgangswerte
  digitalWrite(output1, output1Value);
  digitalWrite(output2, output2Value);
  digitalWrite(output3, output3Value);
  digitalWrite(output4, output4Value);
}

Das funktioniert aber nicht. Die Logik wird nicht wie erwartet im Simulator umgesetzt. Ist der Code fehlerhaft oder mag der Simulator nicht richtig arbeiten?

Bild zum Beitrag
Cplusplus, Code, Arduino Uno
Kann mir jemand mit dem programmieren helfen?

Ich habe den Arduino-Doodlebot ( ein roboter der malen kann ) gebaut und versuche nun ihn so zu programmieren, dass er "HALLO" schreibt, jedoch schreibt er statt "HALLO" einfach ein H und dann noch eins undso weiter (er hört nicht auf ).

Hier ist der Code:

//

#include <Servo.h>

// setup servo

int servoPin = 8;

int PEN_DOWN = 90; // angle of servo when pen is down

int PEN_UP = 180;   // angle of servo when pen is up

Servo penServo;

float wheel_dia=82; //    # mm (increase = spiral out)

float wheel_base=112; //    # mm (increase = spiral in, ccw)

int steps_rev=128; //        # 512 for 64x gearbox, 128 for 16x gearbox

int delay_time=6; //         # time between steps in ms

// Stepper sequence org->pink->blue->yel

int L_stepper_pins[] = {12, 10, 9, 11};

int R_stepper_pins[] = {4, 6, 7, 5};

int fwd_mask[][4] =  {{1, 0, 1, 0},

                      {0, 1, 1, 0},

                      {0, 1, 0, 1},

                      {1, 0, 0, 1}};

int rev_mask[][4] =  {{1, 0, 0, 1},

                      {0, 1, 0, 1},

                      {0, 1, 1, 0},

                      {1, 0, 1, 0}};

void setup() {

  randomSeed(analogRead(1));

  Serial.begin(9600);

  for(int pin=0; pin<4; pin++){

    pinMode(L_stepper_pins[pin], OUTPUT);

    digitalWrite(L_stepper_pins[pin], LOW);

    pinMode(R_stepper_pins[pin], OUTPUT);

    digitalWrite(R_stepper_pins[pin], LOW);

  }

  penServo.attach(servoPin);

  Serial.println("setup");

 

  penup();

 

  delay(1000);

}

void loop(){ // draw a calibration box 4 times

  pendown();

  for(int x=0; x<12; x++){

    pendown();

    forward(80);

    backward(40);

    right(90);

    forward(30);

    left(90);

    forward(40);

    backward(80);

    penup();

    right(90);

    forward(10);

    left(90);

    pendown();

    forward(80);

    right(90);

    forward(30);

    right(90);

    forward(40);

    right(90);

    forward(30);

    backward(30);

    left(90);

    forward(40);

    left(90);

    penup();

    forward(10);

    left(90);

    pendown();

    forward(80);

    backward(80);

    right(90);

    forward(30);

    penup();

    forward(10);

    left(90);

    pendown();

    forward(80);

    backward(80);

    right(90);

    forward(30);

    penup();

    forward(10);

    left(90);

    pendown();

    forward(80);

    left(90);

    forward(30);

    left(90);

    forward(80);

    left(90);

    forward(30);

    penup();

    left(90);

    forward(100);

    penup();

  }

  penup();

  done();      // releases stepper motor

  while(1);    // wait for reset

}

// ----- HELPER FUNCTIONS -----------

int step(float distance){

  int steps = distance * steps_rev / (wheel_dia * 3.1412); //24.61

  /*

  Serial.print(distance);

  Serial.print(" ");

  Serial.print(steps_rev);

  Serial.print(" ");  

  Serial.print(wheel_dia);

  Serial.print(" ");  

  Serial.println(steps);

  delay(1000);*/

  return steps;  

}

void forward(float distance){

  int steps = step(distance);

  Serial.println(steps);

  for(int step=0; step<steps; step++){

    for(int mask=0; mask<4; mask++){

      for(int pin=0; pin<4; pin++){

        digitalWrite(L_stepper_pins[pin], rev_mask[mask][pin]);

        digitalWrite(R_stepper_pins[pin], fwd_mask[mask][pin]);

      }

      delay(delay_time);

    }

  }

}

void backward(float distance){

  int steps = step(distance);

  for(int step=0; step<steps; step++){

    for(int mask=0; mask<4; mask++){

      for(int pin=0; pin<4; pin++){

        digitalWrite(L_stepper_pins[pin], fwd_mask[mask][pin]);

        digitalWrite(R_stepper_pins[pin], rev_mask[mask][pin]);

      }

      delay(delay_time);

    }

  }

}

void right(float degrees){

  float rotation = degrees / 360.0;

  float distance = wheel_base * 3.1412 * rotation;

  int steps = step(distance);

  for(int step=0; step<steps; step++){

    for(int mask=0; mask<4; mask++){

      for(int pin=0; pin<4; pin++){

        digitalWrite(R_stepper_pins[pin], rev_mask[mask][pin]);

        digitalWrite(L_stepper_pins[pin], rev_mask[mask][pin]);

      }

      delay(delay_time);

    }

  }  

}

void left(float degrees){

  float rotation = degrees / 360.0;

  float distance = wheel_base * 3.1412 * rotation;

  int steps = step(distance);

  for(int step=0; step<steps; step++){

    for(int mask=0; mask<4; mask++){

      for(int pin=0; pin<4; pin++){

        digitalWrite(R_stepper_pins[pin], fwd_mask[mask][pin]);

        digitalWrite(L_stepper_pins[pin], fwd_mask[mask][pin]);

      }

      delay(delay_time);

    }

  }  

}

void done(){ // unlock stepper to save battery

  for(int mask=0; mask<4; mask++){

    for(int pin=0; pin<4; pin++){

      digitalWrite(R_stepper_pins[pin], LOW);

      digitalWrite(L_stepper_pins[pin], LOW);

    }

    delay(delay_time);

  }

}

void penup(){

  delay(250);

  Serial.println("PEN_UP()");

  penServo.write(PEN_UP);

  delay(250);

}

void pendown(){

  delay(250);  

  Serial.println("PEN_DOWN()");

  penServo.write(PEN_DOWN);

  delay(250);

}

Computer, Cplusplus, CPP, Programmiersprache, Algorithmus, C (Programmiersprache), Arduino Uno
CPP Anfänger Code hilfe?
#include <iostream>
#include <cmath>
using namespace std;


double Addition(double zahl1, double zahl2)
{
    cout << "Gebe die erste Zahl ein, die addiert werden soll: ";
    cin >> zahl1;
    cout << "Gebe die zweite Zahl ein, die addiert werden soll: ";
    cin >> zahl2;
    double Summe = zahl1 + zahl2;
    cout << "Die Summe ist: " << Summe << endl;
    return zahl1 + zahl2;
}


double Subtraktion(double zahl1, double zahl2)
{
    cout << "Gebe die erste Zahl ein, die subtrahiert werden soll: ";
    cin >> zahl1;
    cout << "Gebe die zweite Zahl ein, die subtrahiert werden soll: ";
    cin >> zahl2;
    double Differenz = zahl1 - zahl2;
    cout << "Die Differenz ist: " << Differenz << endl;
    return zahl1 - zahl2;
}


double Multiplikation(double zahl1, double zahl2)
{
    cout << "Gebe die erste Zahl ein, die multipliziert werden soll: ";
    cin >> zahl1;
    cout << "Gebe die zweite Zahl ein, die multipliziert werden soll: ";
    cin >> zahl2;
    double Produkt = zahl1 * zahl2;
    cout << "Das Produkt ist: " << Produkt << endl;
    return zahl1 * zahl2;
}


double Division(double zahl1, double zahl2)
{
    cout << "Gebe die erste Zahl ein, die dividiert werden soll: ";
    cin >> zahl1;
    cout << "Gebe die zweite Zahl ein, die dividiert werden soll: ";
    cin >> zahl2;
    double Qoutient = zahl1 / zahl2;
    cout << "Der Quotient ist: " << Qoutient << endl;
    return zahl1 / zahl2;
}


double Potenz()
{
    double Basis;
    double Potenz;
    double Ergebnis;
    cout << "Gebe die Basis ein" << endl;
    cin >> Basis;
    cout << "Gebe die Potenz ein" << endl;
    cin >> Potenz;
    Ergebnis = pow(Basis, Potenz);
    cout << "Die Potenz ist: " << Ergebnis << endl;
    return pow(Basis, Potenz);
}


int main()
{
    char Rechenoperator;
    cout << "UNIVERSAL RECHNER" << endl;
    cout << "Bitte wähle die Rechenoperation aus" << endl;
    cout << "Addition(+)\nSubtraktion(-)\nMultiplikation(*)\nDivision(/)" << endl;
    cout << "Potenz(^)\nWurzel(<)\nProzent(%)" << endl;
    cin >> Rechenoperator;


    switch (Rechenoperator)
    {
    case '+':
        Addition(zahl1, zahl2);
        break;
    case '-':
        Subtraktion(zahl1, zahl2);
        break;
    case '*':
        Multiplikation(zahl1, zahl2);
        break;
    case '/':
        Division (zahl1, zahl2);
        break; 
    case '^':
        Potenz(Basis, Potenz);
        break;


    default:
        cout << "Ungültige Eingabe" << endl;
    }


    return 0;
}

main.cpp: In function ‘int main()’:
main.cpp:75:18: error: ‘zahl1’ was not declared in this scope
   75 |         Addition(zahl1, zahl2);
      |                  ^~~~~
main.cpp:75:25: error: ‘zahl2’ was not declared in this scope
   75 |         Addition(zahl1, zahl2);
      |                         ^~~~~
main.cpp:87:16: error: ‘Basis’ was not declared in this scope
   87 |         Potenz(Basis, Potenz);
Cplusplus, Array, Code, CPP, Programmiersprache, Visual Studio, Algorithmus
C++ Code für LGS?

Ich möchte mit einen C++-Programm alle LGS lösen können (endlich, unendlich und keine Lösungen). Ich habe auch einen Code. Endlich viele und keine Lösungen kann er recht gut (bisher), aber unendlich viele Lösungen kann er nicht anzeigen lassen (soll dann auch t einführen, nicht einfach einen Wert einsetzen).

Wo liegt der Fehler?

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

// Funktion zur Berechnung des Determinanten einer Matrix
double determinant(vector<vector<double>>& matrix, int n) {
  double det = 0;

  if (n == 1) {
    return matrix[0][0];
  }

  if (n == 2) {
    return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
  }

  for (int i = 0; i < n; i++) {
    vector<vector<double>> subMatrix(n - 1, vector<double>(n - 1));

    for (int j = 1; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (k < i) {
          subMatrix[j - 1][k] = matrix[j][k];
        }
        else if (k > i) {
          subMatrix[j - 1][k - 1] = matrix[j][k];
        }
      }
    }

    det += pow(-1, i) * matrix[0][i] * determinant(subMatrix, n - 1);
  }

  return det;
}
 
// Funktion zur Durchführung der Gauss-Jordan-Elimination
void gaussJordan(vector<vector<double>>& matrix, vector<double>& constants) {
  int n = matrix.size();

  for (int i = 0; i < n; i++) {
    int maxIndex = i;

    for (int j = i + 1; j < n; j++) {
      if (abs(matrix[j][i]) > abs(matrix[maxIndex][i])) {
        maxIndex = j;
      }
    }

    if (maxIndex != i) {
      swap(matrix[maxIndex], matrix[i]);
      swap(constants[i], constants[maxIndex]); // Korrektur hier
    }

    double factor = matrix[i][i];

    for (int j = i; j < n; j++) {
      matrix[i][j] /= factor;
    }

    constants[i] /= factor;

    for (int j = 0; j < n; j++) {
      if (j != i) {
        double factor = matrix[j][i];

        for (int k = i; k < n; k++) {
          matrix[j][k] -= factor * matrix[i][k];
        }

        constants[j] -= factor * constants[i];
      }
    }
  }
}

// Funktion zur Überprüfung der Invertierbarkeit und zur Lösung des LGS
vector<double> solveLinearSystem(vector<vector<double>>& matrix, vector<double>& constants) {
  int n = matrix.size();
  double det = determinant(matrix, n);

  // Überprüfung auf unendlich viele Lösungen
  bool allZero = true;

  for (double constant : constants) {
    if (constant != 0) {
      allZero = false;
      break;
    }
  }

  if (det == 0) {
    if (allZero) {
      cout << "Das LGS hat unendlich viele Lösungen." << endl;
      return {}; // Leerer Vektor, da unendlich viele Lösungen existieren
    }
    else {
      cout << "Das LGS hat keine Lösung." << endl;
      return {}; // Leerer Vektor, da keine Lösung existiert
    }
  }

  // Anwendung der Gauss-Jordan-Elimination
  gaussJordan(matrix, constants);

  vector<double> solution(n);

  for (int i = 0; i < n; i++) {
    solution[i] = constants[i];
  }

  return solution;
}

int main() {
  vector<vector<double>> matrix = {
    { 1, -3,  5 },
    { 2, -5,  12 },
    { 3, -11,  11 }
  };
  vector<double> constants = { 2, 1, 12 };

  vector<double> solution = solveLinearSystem(matrix, constants);

  if (!solution.empty()) {
    cout << "Die Lösung des LGS ist: ";

    for (int i = 0; i < solution.size(); i++) {
      cout << "x" << i + 1 << " = " << solution[i] << ", ";
    }

    cout << endl;
  }
  else {
    cout << "Das LGS hat keine Lösung." << endl;
  }

  return 0;
}
Schule, Mathematik, programmieren, Cplusplus, Mathematiker, Programmiersprache, Algorithmus, lineare Gleichungssysteme
Wie kann ich mit C++ Textdateien in einem Container speichern?

Ich habe ein Problem mit meinem Programm. Ich möchte eine Textdatei in meinem selbst definierten Container speichern, doch Ich weiß nicht, wo das Problem ist.

#include <iostream>
#include <filesystem>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>

using namespace std;

template <typename Template>
class Container {
  private:
    Template* Eigenschaft1;
    size_t Größe;
    size_t Speichergröße;
  public:
    Container(size_t ContainerGröße = 10000) : Größe(0), Speichergröße(ContainerGröße) {
      Eigenschaft1 = new Template[Speichergröße];
    }

    void Speichern(const Template& Wert) {
      for (int i = 0; i < Größe; i++) {
        Eigenschaft1[Größe++] = Wert;
      }
    }
};

class Personal {
  private:
    string Name;
    string Position;
    int Alter;
    double Gehalt;
  public:
    Personal();

    void Speicherung(string Name, string Position, int Alter, double Gehalt, Container<ofstream>& Container);

    void Ausgabe();

    void Hinzufügen(Container<ofstream>& Container);
};

Personal::Personal() {
  Name = Name;
  Position = Position;
  Alter = Alter;
  Gehalt = Gehalt;
}

void Personal::Speicherung(string Name, string Position, int Alter, double Gehalt, Container<ofstream>& Container) {
  string Entscheidung;
  ofstream Datei1("Datei.csv");
  Datei1 << "Name: " << Name << endl;
  Datei1 << "Position" << Position << endl;
  Datei1 << "Alter: " << Alter << endl;
  Datei1 << "Gehalt: " << Gehalt << endl;

  cout << "M\u00F6chten sie die Person speichern: ";

  if (Entscheidung == "Ja") {
    Container.Speichern(Datei1);
  }
  else if (Entscheidung == "Nein") {
  }
}

void Personal::Ausgabe() {
  ifstream Datei1("Datei.csv");
  getline(Datei1, Name);
  Datei1 >> Position;
  Datei1 >> Alter;
  Datei1 >> Gehalt;
  cout << "Name: " << Name << endl;
  cout << "Alter: " << Alter << endl;
  cout << "Beruf: " << Position << endl;
  cout << "Gehalt: " << Gehalt << endl;
}

void Personal::Hinzufügen(Container<ofstream>& Container) {
  Personal P;
  cout << "Bitte geben side den Namen ein: ";
  cin >> P.Name;
  cout << "Bitte geben sie den Beruf an: ";
  cin >> P.Position;
  cout << "Bitte geben sie das Alter der Person an: ";
  cin >> P.Alter;
  cout << "Bitte geben sie das Gehalt der Person an: ";
  cin >> P.Gehalt;
  Speicherung(P.Name, P.Position, P.Alter, P.Gehalt, Container);
}

template <typename T>
int main() {
  string Passwort1 { "Erfolg1234" };
  string Passwort2;
  Container<ofstream> Container;
  Personal P;
  int Entscheidung;
  cout << "Geben sie das Passwort ein: ";
  cin >> Passwort2;

  if (Passwort2 == Passwort1) {
    cout << "Wollen sie eine neue Position hinzufügen (1)" << endl;
    cout << "Wollen sie auf eine Position zugreifen (2)" << endl;
    cin >> Entscheidung;

    if (Entscheidung == 1) {
      P.Hinzufügen(Container);
    }
    else if (Entscheidung == 2) {
      P.Ausgabe();
    }
  }
  else {
    cout << "Falsches Passwort" << endl;
  }
}
Cplusplus, Code, CPP, Programmiersprache, Visual Studio
C++ - "Funktion nimmt keine 0 Argumente." Behebung?

Schönen guten Abend!

Das Problem ist etwas anders als in der Frage, jedoch ist es für mich schwer dies in einem Satz zu erklären. Nun erstmal mein C++ Quellcode zum Verständnis:

#include <iostream>


int numbCount(int count);


int main()
{
	int num1 = numbCount();
	int num2 = numbCount(2);

  ...

}
  
  int numbCount(int count = 1) 
  {
  	int userNumb;
  	std::cout << "Please enter the " << count << ". integer: ";
  	std::cin >> userNumb;
  
  
  	return userNumb;
  }

Also, wie man sehen kann hat meine Funktion numbCount() Parameter. Außerdem bevorzuge ich es die Funktionsdefinition unter der Main() Funktion zu schreiben.

int numbCount(int count);

int main()
{
...
}

int numbCount(int count = 1)
{
...
}

Somit muss ich die Funktionsdeklaration über der Main() Funktion "erwähnen", damit ja der Compiler weiß, dass die Definition noch kommt. Dies scheint in diesem Fall bei einer Funktion mit Standardparameter für VS ein Problem darzustellen - Fehler "numbCount: function does not take 0 arguments".

Wenn ich jedoch die Funktionsdefinition von "numbCount()" über die Main() Funktion packe, funktioniert alles normal.

int numbCount(int count = 1)
{
...
}

int main()
{
...
}

Habe bereits versucht den Parameter in der "erwähnten" Funktionsdeklaration zwischen den runden Klammern ebenfalls einzufügen, also

int numbCount(int count = 1);

jedoch kommt dann der Fehler 'numbCount': redefinition of default argument: parameter 1

FRAGE:
Gibt es eine Möglichkeit in einer Funktion Standardparamter (also wenn kein Wert in Funktionsaufruf zwischen Klammern angegeben -> standardm. Wert 1) zu haben aber trotzdem die Funktionsdefinition unter der Main() Funktion zu belassen?

int numbCount(int count);

int main()
{
    int num1 = numbCount(); // Kein Wert angegeben - Std. Wert 1
    int num2 = numbCount(2);
}

int numbCount(int count = 1)
{
...
}

Vielen Dank und einen schönen Abend! :)

Computer, Software, Programm, programmieren, Anwendung, Funktion, Cplusplus, Anwendungsentwicklung, Argumente, Code, CPP, Fehlerbehebung, fehlercode, Programmiersprache, Softwareentwicklung, Visual Studio, Fehlermeldung, Parameter

Meistgelesene Beiträge zum Thema Cplusplus