LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-04-2006, 03:39 PM   #16
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47

I thank Muha and /bin/bash for the replies. I must study what you have written. It will take another 48 hours or so. Because I am studying for a test in Russian. I have a test on Thursday. I am studying Russian nowadays.

So after the test, I will write to you all again.
 
Old 04-10-2006, 07:49 AM   #17
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
I have a file named ' lixt1.txt' on my system.
--------------------------------------------



#!/bin/bash
$ test='head -1 lixt1.txt'
$ echo $test
Mary Wednesday
$ echo ${test/ */}
Mary
$ echo ${test/Mary/Joe}
Joe Wednesday
$ echo ${test/W*/Joe}
Mary Joe

I created the above file and named it 'unwanted5'.


-----------------------------------------------------


[nissanka@c83-250-104-186 ~]$ chmod 755 unwanted5
[nissanka@c83-250-104-186 ~]$ ./unwanted5
./unwanted5: line 4: $: command not found
./unwanted5: line 5: $: command not found
./unwanted5: line 6: Mary: command not found
./unwanted5: line 7: $: command not found
./unwanted5: line 8: Mary: command not found
./unwanted5: line 9: $: command not found
./unwanted5: line 10: Joe: command not found
./unwanted5: line 11: $: command not found
./unwanted5: line 12: Mary: command not found
[nissanka@c83-250-104-186 ~]$


What is the problem?
-----------------------------



[nissanka@c83-250-104-186 ~]$ cat lixt1.txt
Mary Wednesday
Joe Monday
Sally Saturday
 
Old 04-10-2006, 11:23 AM   #18
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You don't put a $ before the commands, e.g. if you want to use echo, the line is just something like

echo $test.

Also, if you're trying to put the output of a command into a variable (like it appears you want to do in the first line), then you can't just use those single quotes as that will put the string "head -1 lixt1.txt" into that variable (i.e. if you echo test, then the output will literally be "head -1 lixt1.txt"). I don't know how to do what it looks like you want to do, I don't really know any shell scripting stuff. Not sure what you're trying to do with the lines that say stuff like "Mary", or "Mary Wednesday" either.

Perhaps if you explain what your script is meant to do, then people can help.
 
Old 04-10-2006, 05:02 PM   #19
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Thanks Nylex for taking time to reply me. I have changed the program. Please read the following:


#!/bin/bash
$ test='head -1 lixt1.txt'
echo $test
Mary Wednesday
echo ${test/ */}
Mary
echo ${test/Mary/Joe}
Joe Wednesday
echo ${test/W*/Joe}
Mary Joe
------------------------------------

The following is the output.

[nissanka@c83-250-104-186 ~]$ ./unwanted5
./unwanted5: line 4: $: command not found

./unwanted5: line 6: Mary: command not found

./unwanted5: line 8: Mary: command not found

./unwanted5: line 10: Joe: command not found

./unwanted5: line 12: Mary: command not found
[nissanka@c83-250-104-186 ~]$

What is the problem now?
 
Old 04-10-2006, 06:40 PM   #20
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Please explain what you are trying to do, as what you have written does not make sense as a program.
As Nylex said, do NOT precede cmds with unneeded chars eg '$'
If you want to save the results of the cmd
head -1 lixt1.txt
into a var eg test do it this way
test=`head -1 lixt1.txt`
NB those are backquotes, NOT single quotes ie (on my keybd) the key at the top left of the main keyblock, also has the '~' symbol on it.
Note also that 'test' is a bash keyword, so do not use it as a var.
Also show us what's in lixt1.txt.
 
Old 04-10-2006, 11:38 PM   #21
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
You want to output the front of the file and place the result in a variable, evidently.

Your problem is that you are using single quotes around your head command, when you need to use backquotes (this character ` the unshifted tilde ~).

also, your variable test should not start with a $ when you are assigning it; you only prefix it with a $ when you use it. Also, no space between the $ and the variable name.

Further, your syntax defines Mary as a command on line 4. Unless you have a command named Mary on your system (I don't have on mine), your script will bomb at that point. Similarly for Joe.
 
Old 04-11-2006, 03:03 AM   #22
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
@Gins: when i posted earlier, the commands i typed should be executed from the commandline and not from a script. When you see things like
Code:
$ test=`head -1 list1.txt`
The $ tells you that the command should be executed as a normal user, and from the commandline. In general: $ <command>
So when you want to run that command, just type on your commandline:
Code:
test=`head -1 list1.txt`
The other variant is # <command>
which tells you that the command should be executed as root user. Don't confuse $ <command> with calling a variable, which can be done like so: ${variable} or in our example ${test} or $test

Now what you wanted to do, is to use those commands in a script. When people post scripts they (hopefully) would include the first line #!/bin/bash, which makes it look something like: (this is the example i posted earlier as a script)
Code:
#!/bin/bash
test=`head -1 list1.txt`
echo $test
echo ${test/ */}
echo ${test/Mary/Joey}
echo ${test/W*/Joey}
So when you see scripts posted like so, you can copy-paste them into a file and run it like you wanted to run ./unwanted5
Try copy-pasting this one and run it, does it work? Start learning more on this site: http://www.linuxcommand.org/ Good luck and post if you have problems
 
Old 04-13-2006, 03:42 PM   #23
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
muha

Today I found time to attend this again.

#!/bin/bash
test='head -1 list1.txt'
echo $test
echo ${test/ */}
echo ${test/Mary/Joe}
echo ${test/W*/Joe}

I named the above as 'unwanted6'.


[nissanka@c83-250-104-105 ~]$ chmod 755 unwanted6
[nissanka@c83-250-104-105 ~]$ ./unwanted6
head -1 lixt1.txt
head
head -1 lixt1.txt
head -1 lixt1.txt

What is the problem now? The words Mary,Joe and Sally have gone to hell.
--------------------------------------------
The following is the 'lixt1.txt' file

[nissanka@c83-250-104-105 ~]$ cat lixt1.txt
Mary Wednesday
Joe Monday
Sally Saturday
 
Old 04-13-2006, 03:55 PM   #24
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You want to store the output of "head -1 list1.txt" in a variable called test, correct? You therefore need to use backticks, i.e. ` and not '. I'm sure this has been mentioned several times :/.
 
Old 04-13-2006, 05:51 PM   #25
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Nylex, thanks for the reply.

test='head -1 list1.txt'

If you look at the file, the line you mentioned is there.

However, there is a flaw which I am not capable to recognize.

Nylex, I am a newbie to this scripting. So I am not good at finding flaws.



My guess is this as follows:

One of the following is a must. Please tell me.

$test='head -1 list1.txt'

OR
test= $ 'head -1 list1.txt'
 
Old 04-13-2006, 08:18 PM   #26
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Code:
test=`head -1 list1.txt`
As already mentioned you must use backticks ` not single quotes '
 
Old 04-14-2006, 08:00 AM   #27
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Thanks dive for the comments.
I didn't know the meaning of the word 'backtick'. I searched the meaning on google. It says just single quotes.

You told me it should be double quotes.

So I changed it. Please read the following:

#!/bin/bash
test="head -1 lixt1.txt"
echo $test
echo ${test/ */}
echo ${test/Mary/Joe}
echo ${test/W*/Joe}


However, it didn't work the way it should work. Please read the following output.

[nissanka@c83-250-104-18 ~]$ vi unwanted6
[nissanka@c83-250-104-18 ~]$ chmod 755 unwanted6
[nissanka@c83-250-104-18 ~]$ ./unwanted6
head -1 lixt1.txt
head
head -1 lixt1.txt
head -1 lixt1.txt
[nissanka@c83-250-104-18 ~]$

What is the problem?
 
Old 04-14-2006, 08:19 AM   #28
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
It is not a double quote, it is this -> `
It's like a single quote but at an angle - you just need to find the right key
 
Old 04-14-2006, 08:52 AM   #29
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Thanks dive for replying me again.

Now it works fine.
 
  


Reply

Tags
commands



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Splitting Text lines? 0aniel Programming 9 11-30-2005 03:08 AM
run script on multiple files statmobile Programming 6 07-16-2004 11:35 PM
Need help with shell script - renaming multiple files NiallC Linux - Newbie 25 07-04-2004 10:45 AM
Joining multiple lines and summing fields elconde Programming 1 02-13-2004 10:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration