bash script argument not outputing line -NULL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash script argument not outputing line -NULL
# 1  
Old 09-26-2011
bash script argument not outputing line -NULL

Hi everybody!

I am writing a script which accepts two arguments one of them being a message. All is working except for the message part. That is, to accept text as an argument.

I have trouble storing and echoing a line of text. It seems that writing a few words and store them in an argument ($1) does not work right off the hat, as it is it, prints only the first word I write because if what I write has spaces then it encounters the null character and the script terminates. EXAMPLE:

If I write : “hi how are you?” It will only print the word “hi”.

Code:
  #!/bin/sh
  echo $1>>file.txt
  text=`tail -5 file.txt`
  echo "$text">file.txt

What I have done to overcome this problem is to echo subsequent arguments like this:

Code:
 echo the message is  '"'$1 $2 $3 $4 $5 $6 $7 $8 $9'"'

But I don`t like it and it is not what I want. I want a full line including “\0” and all! I have looked at grep and cat but I have been unsuccessful at finding a solution. Can somebody point me in the right direction?


Bluetxxth
# 2  
Old 09-26-2011
Compare these two runs:
Code:
./yourscript.sh Hello World, how are you?
./yourscript.sh "Hello World, how are you?"

# 3  
Old 09-26-2011
HI Pludi thank you much for your answer.... !!!

It works !!! Shall I assume that I just have to put the citation marks and that that was all that happened?

Regards,

bluetxxth
# 4  
Old 09-26-2011
Yes. The shell splits the command line based on whitespace characters (space, tab, ...), and thus treats all tokens ("words") as separate arguments. When using quotes (either double or single), that special meaning is removed, and the whitespaces are used as part of the argument.
# 5  
Old 09-26-2011
I see... however, it just occurs to me ... if the double quotes cannot be "induced " for example something like when one writes with back quotes to expand what is inside them.

Code:
 `cat file.txt`

# 6  
Old 09-26-2011
Not when reading, but when printing the contents again. Assume you have a file "file.txt" with the following contents:
Code:
This  is a test	with   variable		whitespaces

It contains single and multiple spaces, and tabs just for fun. Now let's read it:
Code:
#!/bin/bash
line=$( cat file.txt ) # $() is the advanced version of backticks
echo $line
echo "$line"

See the difference? In the first output, echo takes all the arguments (more than one, as the shell split on the whitespaces), and outputs them with the default concatenation character (first character in $IFS, by default a single space). The second line, however, passes the string as it is to echo, as a single argument, and all the special characters in it are preserved.
# 7  
Old 09-27-2011
Oh okay.... I see the point.... Thnx so much!!!

BR,

Blue
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash argument not expanding in script

I pass an argument to bash as run. The first command in green executes as expected, however the second in blue fails as the $run does not expand. I tried to escape the variable with \ thinking the quotes were making the literal translation and also "${run}" but both did not work to expand the... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Turn null value into 0 with a Bash Script

Hi, I'm trying to make a bash script that if a null value is returned then to output the value 0. I would like a script to search the 'top' tree and return the CPU value of a process, however if the process is not running for it to return 0 or another identifier. top -b -n1 | awk '/... (4 Replies)
Discussion started by: Goomie
4 Replies

3. UNIX for Dummies Questions & Answers

How to pass command line argument in shell script?

I need to write a shell script, when I run that script I should pass those arguments if not, then script should not run and pass the error message like invalid option - - should pass the argument. and Exit from the script (8 Replies)
Discussion started by: Nsharma3006
8 Replies

4. Shell Programming and Scripting

How to pass command line argument in shell script?

I need to write a shell script, when I run that script I should pass those arguments if not, then script should not run and pass the error message like invalid option - - should pass the argument. and Exit from the script https://www.unix.com/images/misc/progress.gif (1 Reply)
Discussion started by: Nsharma3006
1 Replies

5. Shell Programming and Scripting

checking first argument for tag in bash script

I have a bash script where I pass an argument ./chris.bash "\argv Test" I want to detect if the user supplied \argv at the start of the argument (3 Replies)
Discussion started by: kristinu
3 Replies

6. Shell Programming and Scripting

Script outputing out numbers when it shouldn't

i suspect the issue is with the IFS part. I have a script that reads a file. the problem here is that, when i run the script, it outputs a bunch of numbers. i know what these numbers are, but i dont understand why they're being sent to the screen. as you can see below, everything should be... (5 Replies)
Discussion started by: SkySmart
5 Replies

7. Shell Programming and Scripting

Passing value as a command line argument in awk script.

I have one working awk command line. Which taking data from the “J1202523.TXT” file and generating the “brazil.dat” file. PFB code. awk '{ DUNS = substr($0,0,9);if ( substr($0,14,3) == "089" ) print DUNS }' J1202523.TXT > Brazil.dat But now I want to pass two parameter as a command line argument... (4 Replies)
Discussion started by: humaemo
4 Replies

8. UNIX for Dummies Questions & Answers

Bash script argument problem

I'm having problems with bash scripts. If a bash script is called with no arguments, I always get "PHIST=!" as the first argument (i.e. this is what $1 equals). Why? Where does this come from, and how can I fix it? Nothing in the bash man pages refer to this mysterious default argument. (2 Replies)
Discussion started by: sszd
2 Replies

9. UNIX for Dummies Questions & Answers

pipe output to script as command line argument

i want to redirect output of one command as the command line argument of another script for example, say i would run this command: find . -xdev -type f -size +4096 -exec ls -al {} \; i wan to be able to do something like: echo +4096 | find . -xdev -type f -size ****** -exec... (3 Replies)
Discussion started by: IMTheNachoMan
3 Replies

10. Shell Programming and Scripting

not null cheking of an argument in perl

Hi, I have to check whether an argument say $ARGV is not null in an if operator. Please let me know the operator. It would be great if you write a psuedo code. Thanks in advance Ammu (4 Replies)
Discussion started by: ammu
4 Replies
Login or Register to Ask a Question