Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


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


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
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
Code:
#!/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
Code:
$ 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 :

Code:
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:
Code:
./start.sh 5 watchdog migration kworker ksoftirqd kthreadd

Thank you
Boris

Last edited by baris35; 06-04-2019 at 09:24 AM..
# 2  
Not sure what you're after, but a construct like

Code:
for KW 
  do echo "#define $KW"
  done

inside your start.sh will yield
Code:
#define watchdog
#define migration
#define kworker
#define ksoftirqd
 #define kthreadd

when called like
Code:
./start.sh watchdog migration kworker ksoftirqd kthreadd

If you need the count, use $#.


Redirect to taste...
This User Gave Thanks to RudiC For This Post:
# 3  
Code:
#/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

These 2 Users Gave Thanks to MadeInGermany For This Post:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
User input while reading from a file
parijat guh
I am not able to capture the user input in this script(bash).There is prompt for user input.Could some one help me capture user input while reading afile? while read line do echo "$i" path1=$line path2=`echo $line|sed s/new_dir/old_dir/` echo "Do you want to replace?";...... UNIX for Dummies Questions & Answers
4
UNIX for Dummies Questions & Answers
reading data from a file to an array
gavin_L
I need some help with this code below, i doesnt know why it will run twice with my function, but my function only got if else, any other way that can read line and put into array? while read line; do read -A array <<<$line n=${#array} for ((i=1;i<$n;i++)); do print...... Shell Programming and Scripting
1
Shell Programming and Scripting
reading yum user input
anilcliff
I am writing a script where it uses yum to install. I need to read the user input for yum ie "y or n". If the user types "y", the script should continue running. If the user types "n" then the whole script should be terminated. line1 line2 yum install package line3 line4 From above,...... Shell Programming and Scripting
5
Shell Programming and Scripting
Reading input from user
vadharah
how do we read input from a user e.g i want to ask a user to enter 6 sets of numbers how do i control information from the user? i have this....... #!/bin/bash echo "Please enter six numbers" read number echo $number >> file1 but this stops after the first number..how can i...... Shell Programming and Scripting
2
Shell Programming and Scripting
Reading in data sets into arrays from an input file.
sidamin810
Hye all, I would like some help with reading in a file in which the data is seperated by commas. for instance: input.dat: 1,2,34,/test for the above case, the fn. will store the values into an array -> data as follows: data = 1 data = 2 data = 34 data = /test I am trying to write...... Shell Programming and Scripting
5
Shell Programming and Scripting

Featured Tech Videos