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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to embed data instead of reading user input from an array?
# 1  
Old 06-04-2019
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  
Old 06-04-2019
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  
Old 06-04-2019
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 Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Open Source

Splitting files using awk and reading filename value from input data

I have a process that requires me to read data from huge log files and find the most recent entry on a per-user basis. The number of users may fluctuate wildly month to month, so I can't code for it with names or a set number of variables to capture the data, and the files are large so I don't... (7 Replies)
Discussion started by: rbatte1
7 Replies

2. UNIX for Dummies Questions & Answers

User input while reading from a file

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?";... (4 Replies)
Discussion started by: parijat guh
4 Replies

3. Shell Programming and Scripting

Reading user input...problem with tab key

Hi all, I have a little problem with my shell script (reading user input, save user input to variable, invisible characters in the log file :() printf "1. What's your file path?" /path/to/my/file read -e FILE I have invisible characters in my log file (e.g. <ESC> or ^G) when I'm... (3 Replies)
Discussion started by: splendid
3 Replies

4. Shell Programming and Scripting

reading data from a file to an array

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... (1 Reply)
Discussion started by: gavin_L
1 Replies

5. Shell Programming and Scripting

reading yum user input

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,... (5 Replies)
Discussion started by: anilcliff
5 Replies

6. UNIX for Dummies Questions & Answers

How to display values from user input array?

Hi all, I wrote a script that reads inputs from user and store in array named "input". The number of elements in the array is not fixed - determined only after user exit the while loop that reads the array values : x=1 echo "Enter first value" read input while } != "exit" ] do ... (1 Reply)
Discussion started by: luna_soleil
1 Replies

7. Shell Programming and Scripting

Reading input from user

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... (2 Replies)
Discussion started by: vadharah
2 Replies

8. Shell Programming and Scripting

sh shell user input and stote it to a array

Dear Friends, I am doing a sh shell script , But I dont have any idea how to read value from user Keyboard and store them to an array .. Is it possible or not I am not also sure in sh shell script ? EX:- #! /bin/sh read DATA echo "DATA -" $DATA echo "DATA -" $DATA echo "DATA... (7 Replies)
Discussion started by: user_prady
7 Replies

9. Shell Programming and Scripting

Reading in data sets into arrays from an input file.

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... (5 Replies)
Discussion started by: sidamin810
5 Replies

10. Shell Programming and Scripting

Reading data into muti-dimentional array - in perl

Just want to learn how these are read into array but I don't seem to get it right what do I go wrong? Below is the sample Thanks input 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 #!/usr/bin/perl open (InFILE,"input"); while (<InFILE>) { @ar = split ; (5 Replies)
Discussion started by: zap
5 Replies
Login or Register to Ask a Question