Hello

I want to make two shell scripts but they shell scripts isnt exactly my language :P

The following is all pseudocode
The first script will run every say 5 minutes:

if /tmp/media/sda1 exists or /tmp/media/sda2 exists or /tmp/media/sda3 exists or /tmp/media/sda4 exists
then do
        variable storage=what ever above first returns true (lets say /tmp/media/sda3)
        //storage=/tmp/media/sda3
        if storage+"/"+.runmescript exist
        then do
        ./runmescript
        end if
end if

thats the first one.

Now we have on the harddrive a file called "someconf" at /etc/ also at /tmp/media/sda3 we have a file called "update". someconf has a line that says "dot=99" while update has a line that says "dot=11"

".runmescript" would be.

load /etc/someconf
     search for "dot=99" in /etc/someconf
          load /tmp/media/sda3/update
             search for "dot=11"
             replace line from update in somesonf
save

how can i do this? thank you!

Dani AI

Generated

Short, practical answer for an ash (POSIX sh) environment — pick the first existing /tmp/media/sda[1-4], run a local script if present, and make that local script merge key=value lines from its update file into /etc/someconf safely.

A simple check-and-run script (schedule with cron: */5 * * * * /path/check-and-run.sh):

#!/bin/sh
# check-and-run.sh — run first .runmescript under /tmp/media/sda[1-4]

storage=""
for d in /tmp/media/sda1 /tmp/media/sda2 /tmp/media/sda3 /tmp/media/sda4; do
  if [ -d "$d" ]; then
    storage="$d"
    break
  fi
done

[ -n "$storage" ] || exit 0

runfile="$storage/.runmescript"
if [ -f "$runfile" ]; then
  if [ -x "$runfile" ]; then
    "$runfile" || printf '%s\n' "Error: $runfile failed" >&2
  else
    sh "$runfile" || printf '%s\n' "Error: $runfile failed" >&2
  fi
fi

An accompanying .runmescript that merges update into /etc/someconf (backups and atomic write):

#!/bin/sh
# .runmescript — merge key=value from same-dir "update" into /etc/someconf

set -e
basedir=$(cd "$(dirname "$0")" && pwd)
update="$basedir/update"
target="/etc/someconf"

[ -f "$update" ] || exit 0

if [ ! -f "$target" ]; then
  cp "$update" "$target"
  exit 0
fi

cp -a "$target" "$target".bak 2>/dev/null || true

awk -F= '
NR==FNR { u[$1]=$0; next }
{ k=$1; if (k in u) { print u[k]; seen[k]=1 } else print }
END { for (k in u) if (!(k in seen)) print u[k] }
' "$update" "$target" > "$target".tmp && mv "$target".tmp "$target"

Notes and tips: had the right idea but used non‑POSIX syntax (function/assignment/tests). In ash use POSIX test forms ([ -f "$file" ]), no $ on left of assignments (storage="$file"), and quote variables. Editing /etc requires root — make backups and use atomic writes to avoid corruption. If BusyBox lacks awk, replace the merge step with a safe shell/sed loop or ensure awk is available.

Recommended Answers

All 3 Replies

Well I think another question to ask what shell you were hoping to code this in. It does make a slight difference. But for the hell of it i'll assume your using the bash shell.

Not 100% sure this will work, I no longer have a unix env to test this in.
Script 1:

  function ur_script($file)
    {
    if [ $file = '/tmp/media/sda1' ] || [ $file = '/tmp/media/sda2' ] || [$file = '/tmp/media/sda3' ] || [ $file = '/tmp/media/sda4']
    then 
         $storage = $file; 
         if [ -f $storage+'/'+'.runme' ] 
            then 
                 ./$storage+'/'+'.runme'
         fi 
    else
        echo "Error: File Not Found"; 
    fi 
    }

Can't help you with the second on though sorry.

thanks anyways soapyillusion :) but a function is not ideal for this

im using ash btw

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.