How do I read & write an integer from a file? [closed] - bash

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need a script or one-liner to read an integer from a file, add 10, set my monitor brightness, and write the new value back to the file. I have it working in AppleScript, but it's rather slow, so was hoping to recreate in bash.
Basically:
Read value X from a file
Increment X by 10
If X > 100 then set X to 100.
Set brightness to X using ddcctl -d 1 -b $X
Write X back to the file (replacing)

Try the following:
#!/usr/bin/env bash
#
# Directory where this script is located
#
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
declare x=$(cat "${DIR}/path/to/file")
x=$((x+10))
if [[ ${x} -gt 100 ]]; then
x=100
fi
ddcctl -d 1 -b ${x}
echo "${x}" > "${DIR}/path/to/file"

Related

Why won't a value in this bash script equal a command? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
When I run this code, the variable "thing" doesn't change its value to the command. I've tried everything and I just can't get it to work. I want thing to equal something like "1 history cd /bin
history cd /home/user/"
#!/bin/bash
val="thing"
function send () {
thing
thing=$(history | tail -n 2)
echo $thing
echo $val
# echo $last
if [ "$val" == *"this"* ]; then
echo "yes"
fi
exit 1
}
send
If you wonder why $(history | tail -n 2) returns nothing, it is because history lists commands previously ran in the current shell.
But your script is a new shell instance, so it does not carry the history of commands you ran before you execute your script.
If you want that, you have to source the script, not execute it. To source, do:
$ . thescript.bash
instead of
$ ./thescripts.bash
instead of this also
$ bash thescript.bash
Note: put your code in https://www.shellcheck.net/ to see syntax issues.

Ask a user for integers until they enter control+a. Then sum up the integers and display the total [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I've been working on some bash scripts and kind of hit a wall.
I need to ask a user to enter a bunch of different integers, and when they press 'control+a'
it'll sum up every integer they entered, like this:
10
10
2
"control+a"
22
I'm not sure where to even start on this.
I really appreciate the help, thank you.
If your terminal supports it:
#!/bin/bash
stty eof ^A
back2default(){ stty eof ^D; }
trap back2default EXIT
declare -i sum=0
while true; do
read -r foo
[[ -z $foo ]] && break
sum+=$foo
done
echo "sum: $sum"

Bash Script to update file progrmatically [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a file with contents like this
adv_dir=2
ami_pro=3
I have a variable $TEMP with value ami_pro, I want to increase the value of $TEMP by 1.
Want a bash script for this.
State of file after updates
adv_dir=2
ami_pro=4
Icky-ish, spooky indirection, but...
$: echo $TEMP
ami_pro
$: echo $ami_pro
4
$: (($TEMP++)) # $TEMP evals to ami_pro, which gets incremented
$: echo $TEMP
ami_pro
$: echo $ami_pro
5
$: let $TEMP++ # same, likely in any arithmetic context
$: echo $ami_pro
6
so...
TEMP=ami_pro # just setting in the env
grep "$TEMP=" file > $TEMP.tmp # grap the line we need to edit
. $TEMP.tmp # souce it to set the var
sed -i "s/$TEMP=${!TEMP}/$TEMP=$((++$TEMP))/" file # in-place edit
You could skip the tempfile with eval "$(grep "$TEMP=" file)" but eval makes me itch.
The sed is an in-place edit of file using double-quotes to allow the OS to pre-process the vars before making the update.
TEMP=ami_pro
so
$TEMP=${!TEMP}
is parsed by the OS into
ami_pro=$ami_pro
Since we sources the line that said ami_pro=3, that gets further parsed into
ami_pro=3
then
$TEMP=$((++$TEMP))
becomes ami_pro=$((++ami_pro)) which processes to
ami_pro=4
all sed gets is the result strings, so by the time sed starts parsing, what it sees is
s/ami_pro=3/ami_pro=4/
Once that edit is handled, we can break out of the loop.
You can use awk for that:
#!/bin/bash
TMP_FILE=$(mktemp)
awk -F= "/^${TEMP}=/ {print \$1 \"=\" \$2 + 1 \"*\"; next} 1" "${1}" > ${TMP_FILE} && mv ${TMP_FILE} "${1}"
rm ${TMP_FILE}
Pass the file with the data as a parameter to this script.

Write a script to get the line number of a file with "while read LINE", but failed [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
#!/bin/sh
num=1
cat $1 | while read LINE
do
num=`expr $num + 1`
done
echo $num
Your script is spawning a sub-shell when you use pipe after useless cat. All the changes to $num made inside sub-shell get lost after while loop ends and you get back to parent shell.
You should initialize num with 0 not 1
It is better to not to use all capital letter variable names to avoid collision with internal shell variables.
Instead of reverse tick you should use $(...) for command substitution.
You should use:
#!/bin/sh
num=0
while read -r line
do
num=$(expr $num + 1)
done < "$1"
echo $num

Bash: Reading lines from a specified file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In Bash, I would like to know how to read lines from a file in a directory i specifiy so that there are no arguements along with running the script. All I have seen if suggestions for running a script with a file given as the arguement rather than one specified.
while read -r line; do
echo "$line"
done < filename
% cat read_line_by_line_example.txt_inside
dir="$1"
[ -f "$dir"/example.txt ] || exit 1
# borrowing from Cyrus
while read -r line; do
echo "$line"
# do watever you want with "$line"
done < "$dir"/example.txt
% sh read_line_by_line_example.txt_inside "a/b/c/d/my dir"
line 1
line b
last line
%

Resources