How to embed data instead of reading user input from an array?

Hello,
I am running under ubuntu1 14.04 and I have a script which is sending given process names to vanish so that I'd see less output when I run most popular tools like top etc in terminal window. In usual method it works.
Whenever I restart the system, I have to enter the same data from keyboard at startup. since it was asking me to enter data from command line, I am unable to run it from systemd/services as a startup process. I'm looking for a solution to embed the keywords into below script:

start.sh

#!/bin/bash
echo "Enter process count: "
read processCount
if ! [[ "$processCount" =~ ^[0-9]+$ ]]
    then
        echo "Sorry integers only"
fi
echo "Enter the process names: "
for (( i=1; i<=processCount; i++ ))
do
    read line
    processList=("${processList[@]}" $line)
done

echo ${processList[@]}

processArray="{"
for (( i=0; i<processCount; i++ ))
do
    processArray+="\"${processList[$i]}\","
done
processArray=${processArray::-1}"}"
echo "#define PROCESS_COUNT $processCount" > hidelib.c
echo "#define PROCESS_LIST $processArray" >> hidelib.c
tail -n +3 "proc_hide.c" >> "hidelib.c"

When I run the script, ./start.sh

$ Enter process count:
> 5
$ Enter the process names: 
> watchdog
> migration
> kworker
> ksoftirqd
> kthreadd

What I wish to accomplish is not convert user input to array, just need to add watchdog , migration and other phrases into this script so that it will run at startup without asking any questions.

I removed all lines but just kept last two lines, and run them like :

echo "#define migration" >> hidelib.c
echo "#define  watchdog" >> hidelib.c
echo "#define  migration" >> hidelib.c
echo "#define  kworker" >> hidelib.c
echo "#define  ksoftirqd" >> hidelib.c
echo "#define  kthreadd" >> hidelib.c
tail -n +3 "proc_hide.c" >> "hidelib.c"

I'd appreciate your recommendation.
Something opposite of this request:
sh shell user input and stote it to a array

Or like this way:

./start.sh 5 watchdog migration kworker ksoftirqd kthreadd

Thank you
Boris

Not sure what you're after, but a construct like

for KW 
  do echo "#define $KW"
  done

inside your start.sh will yield

#define watchdog
#define migration
#define kworker
#define ksoftirqd
 #define kthreadd

when called like

./start.sh watchdog migration kworker ksoftirqd kthreadd

If you need the count, use $# .

Redirect to taste...

1 Like
#/bin/bash
if [ $# -eq 0 ]
then
   echo "usage:
   $0 procnames ..."
  exit 1
fi
{
echo "#define PROCESS_COUNT $#" 
printf "#define %s\n" "$@"
tail -n +3 "proc_hide.c"
} > hidelib.c
2 Likes