Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sed instead of lua #42

Open
pickfire opened this issue Feb 13, 2015 · 4 comments
Open

Use sed instead of lua #42

pickfire opened this issue Feb 13, 2015 · 4 comments

Comments

@pickfire
Copy link

Hi, I doesn't know what does these lua means but I think I can replace it with something else which is shorter and reduce the dependencies.

set_config_var() {
  lua - "$1" "$2" "$3" <<EOF > "$3.bak"
local key=assert(arg[1])
local value=assert(arg[2])
local fn=assert(arg[3])
local file=assert(io.open(fn))
local made_change=false
for line in file:lines() do
  if line:match("^#?%s*"..key.."=.*$") then
    line=key.."="..value
    made_change=true
  end
  print(line)
end
if not made_change then
  print(key.."="..value)
end
EOF
mv "$3.bak" "$3"
}

I think that function is to change arm_freq=700 to arm_freq=900. You can use sed in that way too:

function set_config_var() {
  grep -q "^$1=" $3 && sed -i "/^$1=/c $1=$2" $3 || echo "$1=$2" >> $3
  return 0  # search $1 and change "$1=*" to "$1=$2" or append "$1=$2" to EOF
}

And for the function to get the config values you can do it with sed too:

get_config_var() {
  lua - "$1" "$2" <<EOF
local key=assert(arg[1])
local fn=assert(arg[2])
local file=assert(io.open(fn))
for line in file:lines() do
  local val = line:match("^#?%s*"..key.."=(.*)$")
  if (val ~= nil) then
    print(val)
    break
  end
end
EOF
}

With sed:

get_config_var() {
  sed -n "s/$1=// p" $2
}

Sorry that I didn't send you a pull request as I don't think I should fork it just for these code. I hope you can reduce the dependencies and shorten the code by using a shorter function. Thanks.
Ivan Tham [email protected]

@alecthegeek
Copy link

The justification is that Lua is always present on Debian and is easier to read (although I use sed in all my scripts, but I'm old skool)

@mobluse
Copy link

mobluse commented May 3, 2015

I'm not sure the SEd-functions does the same as the Lua-functions. I don't know Lua. I think the Lua-functions are better now because they seem to uncomment a commented key-value pair. With these suggested functions with SEd the commented key-value pairs would remain commented and the key-value pairs are inserted at the end of the file. This leads to a more difficult-to-read configuration file, where the documentation in comments is in one place and the key-value pair in another. One could replace the Lua-functions with more complex Sed- or AWK-functions. I think this should be done because one should not require that Lua is installed. Also, Lua is not that easy to understand, e.g. what does "^#?%s*" mean?

BTW I found this useful discussion: http://stackoverflow.com/questions/2464760/modify-config-file-using-bash-script

@alecthegeek
Copy link

  1. Lua is always present in Raspian
  2. sed and Lua regex are equally opaque. #? means optionally one comment char. It would be same in sed

Having said all that I use the following style in my Pi scripts

setConfigOption() {
  CONFIG_FILE=/boot/config.txt
  if grep "$1=" $CONFIG_FILE ; then
    sudo sed --in-place -re "s/^#?$1=.+$/$1=$2 #$UPDATE_LABEL/" $CONFIG_FILE
  else
    echo $1=$2'  #'$UPDATE_LABEL| sudo tee -a $CONFIG_FILE
  fi
}

@pickfire
Copy link
Author

pickfire commented May 4, 2015

^# $1 could also mean one commented char. To uncomment, use:

sed -i "s/^# $1/$1/" $CONFIG_FILE

^ means the first character in the line and # is the first character, $ is the last character in the line.

I prefer to have a short one:

function set_config_var() {
  grep -q "^$1=" $3 && sed -i "/^$1=/c $1=$2" $3 || echo "$1=$2" >> $3
  return 0  # search $1 and change "$1=*" to "$1=$2" or append "$1=$2" to EOF
}

set_config_var('cpu_freq', 1000, /boot/config.txt)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants