How do I correctly read in a file with sh scripting and using it in an if statement? [duplicate] - linux

This question already has answers here:
Assignment of variables with space after the (=) sign?
(4 answers)
Assing a variable and use it inside of if statement shell scripting
(2 answers)
Difference between sh and Bash
(11 answers)
Closed 3 years ago.
So there is a kernel adiutor for android, that let's you add custom controls with shell scripting. I'm trying to add a switch, but I have trouble setting the switch up correctly, when it's active, and when it's not. So there is a (text)file I'm trying to read (you will see it in the code), whether it's 0 or 1 inside, and that determines the switch on-off state.
I've tried with cat, read, everything, but honestly I think the problem is that I'm not familiar with sh scripting, and there is a problem with my syntax. Sometimes the script won't return anything when using cat. Also, su is available so that's not a problem, also the file has the correct permissions.
#!/system/bin/sh
var= $(<sys/class/lcd/panel/mdnie/hdr)
if ( "$var" = 0) then
echo 0
else echo 1
fi
The problem with my code is that right now it returns 1 (on), even when the file has a 0.

When assigning a variable in shell, there must be no space after the assignment sign. Also, make sure you use the correct syntax for conditions (and be aware of whitespace sensitivity):
var=$(cat sys/class/lcd/panel/mdnie/hdr)
if [ "$var" = "0" ]; then
# if [ "$var" -eq 0 ], if you want numeric comparison (won't really matter here)
echo 0
else
echo 1
fi

Related

Why is bash string comparator only working when given substring? [duplicate]

This question already has answers here:
Case insensitive comparison of strings in shell script
(14 answers)
Closed 1 year ago.
I am trying to add users from a csv file to a group in a bash script running on CentOS 8. The group names are "Faculty" and "Students", which I am forcing them to be lowercase. The following did not work. It defaults to the "else" clause, even when $groupName is "Faculty" (I would "echo" before the if statement).
if [ "$groupName" = "Faculty" ]
then
goodGroup="faculty"
else
goodGroup="student"
fi
However, it worked when I gave it a substring of only the capital letter:
if [ "${groupName:0:1}" = "F" ]
then
goodGroup="faculty"
else
goodGroup="student"
fi
Using the second method gives me the outcome I need, I am just curious why the first bit of code did NOT work. All the answers I've seen on StackOverflow say that's the syntax for comparing strings, so I can't see what I'm doing wrong.
Ways to force a variables value to lowercase in bash without having to check for specific values:
#!/usr/bin/env bash
# Using declare to give a variable the lowercase attribute
declare -l groupName1
groupName1=Faculty
printf "%s\n" "$groupName1"
# Using parameter expansion
groupName2=FACULTY
printf "%s\n" "${groupName2,,}" # or "${groupName2#L}"

Shell script won't run properly when re-assigning variable [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Variable variable assignment error -"command not found"
(1 answer)
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 4 years ago.
So I've got a shell script to do some lazy stuff for if the directory isn't changing for a user. It's below. Essentially, it should be an if statement that if the user enters "default" for the directory, it'll pull them to the default directory for the files. However, I'm getting a command not found on line 16, which is the reassignment statement.
The entire if statement below:
if [ $directory = "default" ];
then
echo Enter your ldap:
read $ldap
$directory = "/usr/local/home/google/${ldap}/Downloads"
fi
I've tried doing it without the dollar sign too...nothing. What's going on here? New to shell, couldn't find this question asked before either.
This is how you should assign a value to a variable in shell:
directory="/usr/local/home/google/${ldap}/Downloads"
No dollar ($) sign.
No space around equal (=) sign.
Also, you should wrap your variables inside double quotes ("). This way, you avoid errors with undefined variables, arguments with spaces, etc.
That gives us:
if [ "$directory" = "default" ]
then
echo "Enter your ldap:"
read $ldap
directory="/usr/local/home/google/${ldap}/Downloads"
fi

Change value of variable insead loop Bash [duplicate]

This question already has answers here:
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 4 years ago.
I'm newer on bash scripting ,I have a global variable that I want to change his value insead a loop in my script but still get an error that commande not found
this my script :
SCRIPT_BASE = "/home/scripts/test-Scripts"
CURRENT_SCRIPT_PATH = ""
declare -a arr=("A" "B" "C" "D")
for i in "${arr[#]}"
do
if [ $i == "A" ]; then
CURRENT_SCRIPT_PATH = $SCRIPT_BASE
echo -e "Current Path : $CURRENT_SCRIPT_PATH"
fi
done
when I run this script I get that CURRENT_SCRIPT_PATH commande not found
Thanks in advance for any help
In bash you should be really cautious about spaces in if conditions but also when you assign a value to a variable.
Replace in your code the following tree lines:
SCRIPT_BASE="/home/scripts/test-Scripts"
CURRENT_SCRIPT_PATH=""
CURRENT_SCRIPT_PATH=$SCRIPT_BASE
If you keep a space after the variable name bash will interpret it as a command and as you do not have commands SCRIPT_BASE, CURRENT_SCRIPT_PATH, CURRENT_SCRIPT_PATH in your current $PATH you have the error command not found that is produced.

Adding one or two directories to PATH variable in linux ( Using Bash script) [duplicate]

This question already has answers here:
Check number of arguments passed to a Bash script
(10 answers)
Add a bash script to path
(5 answers)
Closed 5 years ago.
So far
I have the following code:
#!/bin/bash
echo "Adding new path...."
if [[$# -eq1] || [$# -eq2]]
then
if [$# -eq2]
then
export PATH=$PATH:/$1:/$2
fi
if [$# -eq1]
then
export PATH=$PATH:/$1
fi
else echo "Incorrect number of parameters. No more than two directories can be added at once."
fi
echo $PATH
exit 0
When I run this script passing it one parameter i get an error:
"./addDir: line 3: [[1: command not found
./addDir: line 3: [1: command not found "
when I run it with 2 parameters instead of "1" it says "2"
What's going on?
You're missing some spaces. Basically, if you're trying to use the [...] construction, you need to have spaces before and after each bracket - think of [ as being the name of a command, in the same way as echo, and ] as being an argument to that command. (In fact, there might actually be a /bin/[ program on your system.) Just as you can't type echofoo and expect it to run the echo program, similarly you can't type [[$# if you expect it to run [.
In your case, you'd need to do things like
if [ $# -eq 2 ]; ...
And for the compound test you're doing in line 3, I don't think you can use [ and ] within the test. In other words, don't use those brackets for grouping; it has to be [ something ] where the something doesn't contain any brackets. Read the relevant section of the bash man page for the full details of what you can put there.
There is also a shell construct [[ ... ]] which does basically the same thing but has different syntax. You could use that instead, but be aware that it's very different from [ ... ].

bash: How can I assemble the string: `"filename=output_0.csv"`

I am using a bash script to execute a program. The program must take the following argument. (The program is gnuplot.)
gnuplot -e "filename='output_0.csv'" 'plot.p'
I need to be able to assemble the following string: "filename='output_0.csv'"
My plan is to assemble the string STRING=filename='output_0.csv' and then do the following: gnuplot -r "$STRING" 'plot.p'. Note I left the words STRING without stackoverflow syntax style highlighting to emphasise the string I want to produce.
I'm not particularly proficient at bash, and so I have no idea how to do this.
I think that strings can be concatenated by using STRING="$STRING"stuff to append to string? I think that may be required?
As an extra layer of complication the value 0 is actually an integer which should increment by 1 each time the program is run. (Done by a for loop.) If I have n=1 in my program, how can I replace the 0 in the string by the "string value" or text version of the integer n?
A safest way to append something to an existing string would be to include squiggly brackets and quotes:
STRING="something"
STRING="${STRING}else"
You can create the "dynamic" portion of your command line with something like this:
somevalue=0
STRING="filename='output_${somevalue}.csv'"
There are other tools like printf which can handle more complex formatting.
somevalue=1
fmt="filename='output_%s.csv'"
STRING="$(printf "$fmt" "$somevalue")"
Regarding your "extra layer of complication", I gather that this increment has to happen in such a way as to store the value somewhere outside the program, or you'd be able to use a for loop to handle things. You can use temporary files for this:
#!/usr/bin/env bash
# Specify our counter file
counter=/tmp/my_counter
# If it doesn't exist, "prime" it with zero
if [ ! -f "$counter" ]; then
echo "0" > $counter
fi
# And if it STILL doesn't exist, fail.
if [ ! -f "$counter" ]; then
echo "ERROR: can't create counter." >&2
fi
# Read the last value...
read value < "$counter"
# and set up our string, per your question.
STRING="$(printf "filename='output_%d.csv'" "${value}")"
# Last, run your command, and if it succeeds, update the stored counter.
gnuplot -e "$STRING" 'plot.p' && echo "$((value + 1))" > $counter
As always, there's more than one way to solve this problem. With luck, this will give you a head start on your reading of the bash man page and other StackOverflow questions which will help you learn what you need!
An answer was posted, which I thought I had accepted already, but for some reason it has been deleted, possibly because it didn't quite answer the question.
I posted another similar question, and the answer to that helped me also answer this question. You can find said question and answer here: bash: Execute a string as a command

Resources