Help Modifying Text With User Input

Hello,

I apologize in advance for my limited UNIX scripting knowledge. I am new to it and really want to learn.

I am trying to write a bash script that updates a config file based on user input.

What is the best method for accomplishing this? I need it to prompt the user for two variables, find the location in the config file, and insert new text with the two variables.

I was thinking I could use sed to find the text in the config file where the new text must be inserted before and replace it with the new text, two variables, and same ending text as before. Example:

echo "1st variable?"
read variable1

echo "2nd variable?"
read variable2

sed -e "s|<the spot in the config file that needs new config>|sometext...$variable2_somemoretext...$variable1\n<the spot in the config file that needs new config>|g" config > config2


This works except variable2 is not inserted into the replace string, only variable1 is. This is the result in config2:

sometext..._somemoretext...$variable1
<the spot in the config file that needs new config>


If anyone can tell me why variable2 isn't working in the sed replace string, or if there's a much better way for accomplishing what I'm after, I'd appreciate any help I can get.


Similar Content



Tips On Using Bash Script To Edit A Windows Text File

I have a bash script executing on an AIX server to a windows share directory to update a windows text file. Most of the file is edited as expected but I have 3 issues that I am not sure how to resolve.

1. Windows text file contains a double backslash (\\) and the \\ is being written back as a single \ which corrupts the file.
2. I am not getting the last line of text in the file written back out.
3. Some lines of text are not written back out completely.

Here is a snippet of my bash script:
cp $Directory/Somefile $Directory/Somefile.$(date +'%Y%m%d')
cat /dev/null > ${HOME}/temp.out

while read -r inpLine; do
patternFound=n
echo ${inpLine} | grep -q 'SOMEPATTERN='
if [ $? = 0 ]; then
patternFound=y
echo "##${inpLine}" >> ${HOME}/temp.out
echo "SOMEPATTERN=newValue" >> ${HOME}/temp.out
fi

if [ ${patternFound} = "n" ]; then
echo ${inpLine} >> ${HOME}/temp.out
fi

done < $Directory/Somefile
mv ${HOME}/temp.out $Directory/Somefile

Misbehaving 'echo' Command On Cygwin

Hi,

I've used unix before, but have just started to play with CYGWIN (1.1.3) on windows XP (32 bit). I'm having trouble getting 'echo' to behave as I would expect.

I've created a script called runstuff which contains these lines....
Code:
#!/bin/bash
# This line works as expected....
echo "the user is <$USER> and the hostname is <$HOSTNAME>"
MYVAR1=bill
MYVAR2=fred
# This lines doesn't.....
echo "my first variable is<$MYVAR1> and my second is <$MYVAR2>"

The output I get is this...
Quote:
$ runstuff
the user is <Mike> and the hostname is <MikesLaptop>
> and my second is <fredl

In the first echo statement it is just echoing a couple of external variables - this works as expected. In the 2nd echo example the two variables are internal, but the displayed output isn't what I'd expect, my guess is that the first part of the output "my first variable is <bill" has somehow been overwritten by the remainder of the string. Or am I missing something?

I have tried exporting the variables, and I've experimented with wrapping them in quotes, but the results are always the same.

Any help gratefully received.

Trouble Configuring Vsftp

Using a Raspberry Pi w/ Debian

Read the manual and several "how-to"s and am getting nowhere

vsftp is running and making a log file. I can get to the Pi with putty or SAMBA

Ultimately want to be able to both(I) ftp in under the "pi" user to access files remotely and (II) have people sending me huge files ftp them in anonymously

Have tried (I) the ftp client in Windows 7 Internet explorer 11, (ii) the ftp client in the Windows 7 "map network drive" UI, and (iii) ES file explorer on my Android phone.

The android phone tells me there is no server there. Windows 7 gets no password challenge, gives no error indication, but does nothing useful. The vsftpd log file shows some attempt by the Windows machine, nothing for the Android.

Config file and log file attached.

The Android is trying to use port 21, the config file mentions something about port 20. Which ports do I need to forward in my router to enable remote access?

Any help appreciated.

Thanks. Jonathan

Regarding .config File

Hello,Everyone
if anyone has the information regarding the config the file,please let me know form where I can get the .config file for ubuntu 14.04.

Thanks and regards
sanjay

Bash Shell Scripting Need Help Figuring Out Process For Homework - JUST WANT DIRECTIO

Ok yes this is a homework assignment BUT I am NOT looking to have the answers given to me. I am in the 6th week of my first Linux class ever and we are in our few weeks of beginning scripting. I have some ideas of what to do or where to start but not many and no one to bounce any ideas off...we are using UNIX Bash shell so any others I have no clue. The scenario is that I need a script that searches all my users home directories for bad words. I need the script to report to the screen certain info like username and word found and path. It should ask a user if it is good or bad and if bad be put into a file of list of bad file names, if good remove from list and no longer flagged by the script. What I have so far is wanting to somehow do a loop. I do know that if I do a grep -r -e kill -e steal /home/* I get a list of what I need. I also know that the list is separated by delimiters which I can pipe to get a variable for the things I need. I also know that I can put it to a file with a > filename.txt

What I have no clue is how to start a loop that would do this...

for each line in filename.txt
UNAME=...
LOC=...
TXT=...
echo "Username: $UNAME, Line with bad word found: $TXT, and Path and file name: $LOC. Is this a BAD file? (Y)"
Read YORN

if ["$YORN" = "Y" ]; then
>> (line of text from grep) badfiles.txt
fi

Next or whatever goes there...sorry if this is crazy I just really need some direction. I am trying to learn so please don't give me the answer...that will do nothing for me and I will not be able to explain the code I came up with.

GPG: How Does A Gpg2Masterkey(THE Private Key) Store All The Info Of Its PubSub Keys?

For one user I generated a gpg2 key-pair. I exported these to ascii-armour text files as master.asc and pub.asc. Then took only the pub.asc onto the keyring of a second user on the same partition. I "signed" a simple text file (say trial.txt) with the master key and also transferred it to the home directory of the second user. The pub key on the second user's keyring could verify the signature on the signed text file (trial.txt.gpg) but could not decrypt it. Also, a file encrypted by the second user with the pubkey could be decrypted by the masterkey. Each key therefore has, embedded in its structure, information about child/parent keys. Can anyone thrown light on the actual method of incorporation of this mutual recognition in the key structures? Note:The same kind of recognition happens with sub-keys derived from the Master-key. Can't find any literature which details. Help pl.

How To Copy File From Remote Host To Local Host Then Delete From Remote Host

I have an expect script to SSH to a remote host and obtain some user inputs and information about the server/network configuration. The responses are saved in a text file that I then need to copy to my local host so that I can read the lines into variables for use in the parent shell script.

Is there a way to do this without needing to enter the username and password for the local host to use function scp? I have tried the following in my expect script to no avail:
Code:
spawn scp $usr@$host:$flnm .
expect {
	-re "(.*)assword:" { 
		send -s "$pswd\r"
	}
}

I have also tried to directly scp the file and enter the username and password to try to debug the issue, and that doesn't work either:
Code:
spawn scp file.txt user@host:file.txt
expect {
	-re "(.*)assword:" {
		send -s "password\r"
	}
	"you sure you want to continue connecting" {
		send -s "yes\r"
		exp_continue
	}
}

In both scenarios I have used exp_internal 1, and there are no errors. But I do not end up with the file on my local host.

Following the copy, I would like to delete the file from the remote host. Any suggestions on how to accomplish this?

Zenity Radiolist Parameters In Variables

Note: Ubuntu 14 latest release

I am a highly experienced unix user with many years of experience. However, I retired 13 years ago so I can't remember some simple things so forgive me for posting here.

I am trying to use the "zenity --list --radiolist" command. Now this requires that the list of items be last and be of the form FALSE <item name> as in FALSE "Choice 1" FALSE "Choice 2" That's easy without variables. It would be zenity --list --radiolist [other options here] FALSE "Choice 1" FALSE "Choice 2 Works fine.

But what I have is a list of choices in an array. Each of these is a string of multiple words and I have tried (nearly?) everything using echo command piped into zenity (supposed to work) to including an array element in the line with all sorts of quotes, single quotes, escaping quotes, etc. I have even included the "FALSE" in the variable so as not to have to generate it. Nothing works. When using the set -x, it shows that the shell has put a single quote around each word which makes each word a separate parameter.

I have searched all sorts of examples of zenity but the requirement for the word "FALSE" between each item while using variables is never shown.

If anyone has a working example of something like this (or any simple alternative:
JTemp[3]="This is a string"
echo "FALSE "${Jtemp[3]} | zenity --list --radiolist --text "Select Topic" --column "Topic" --column "Pick one" which should display a radiobox with one entry, I would appreciate it. I could then expand it to include all the array elements.

I know I am doing something simple wrong. I just can't figure it out.

Note that I am writing this on a windows machine and using a non-internet-connected linux machine for my work so if I typed the command line wrong, it may be a problem here and not on the real machine.

Thanks for any help.

Phpmyadmin Config Issues

Hello there,


I have VPS(Centos), I have installed the phpmyadmin on it..

I have also edited the config file of phpmyadmin and entered the vps ip address, did not touched anything else..

Now when i am trying to login the phpmyadmin in the browser, It is showing me that I dont have permission..

please help..

I have successfully installed and I can access phpmyadmin on My Desktop with the same config.

I dont know understand where it went wrong.

:wq

Would Like To "echo" A Line, "read" Reply, Then Execute The Reply: How In Bash?

Apologies. I've been out of Unix for 14 years, and the gears have rusted. Also, Unix has changed - enough similarities to memory that I'm pretty comfy though.

Glad to see lots of info available via google. However changes are going to take getting used to. Have set up a testbed of 3 nearly identical machines, hoping to make an (at home) private network where I can automate installs and set-ups using NIS and NFS (Similar to work I once did).

Have found useful scripts interspersed with comments, would like to copy and paste them to a bash script that:

Reads the line.
Echos the line.
Reads the reply.
Executes what is typed.

Essentially, by making myself type (or copy & paste) the commands I think I'll become familiar.

Figured I'd use a spreadsheet, select the file, and paste into column B of the spreadsheet. In column A I'd paste:

echo "

and in column C I'd paste:

"

Then I'd cut and paste into a text document, insert the lines:

read A
$A

behind every command necessary to the actual task.

Figured I'd have to escape the ' and ".

Ran a test (test.bash):

#!/bin/bash
for i in `seq 1 10`;
do
echo "Enter a command:"
read A
echo "Trying $A"
$A
done

works fine when I type

ls -alg

but not so when I type

ls -alg | grep dwrx

so I'm wondering if someone has a suggestion?

Thanks for any ideas.