Click to See Complete Forum and Search --> : shell script problem, please help
Lawes
03-03-2003, 10:23 AM
Dear all,
I not famous in shell script, but I want to make a simple shell script in order to make a number of pref file base on email address.
I try to write the script as follow
#!/bin/bash
#cat /root/temp.txt | awk '{ print $1 $2 $3 }'
echo "chosen_theme=../themes/plain_blue_theme.php" > $2.pref
echo "Show_html_defauolt=1" >> $2.pref
echo "javascript_on=1" >> $2.pref
echo "full_name=$3" >> $2.pref
echo "email_address=$1.pref" >> $2.pref
echo "use_signature=0" >> $2.pref
echo "prefix_sig=0" >> $2.pref
echo "javascript_setting=2" >> $2.pref
echo "show_num=15" >> $2.pref
echo "alt_index_colors=1" >> $2.pref
echo "page_selector=1" >> $2.pref
echo "page_selector_max=10" >> $2.pref
echo "wrap_at=86" >> $2.pref
echo "editor_size=76" >> $2.pref
echo "use_javascript_addr_book=0" >> $2.pref
echo "include_self_reply_all=1" >> $2.pref
echo "show_xmailer_default=0" >> $2.pref
echo "attachment_common_show_images=1" >> $2.pref
echo "pf_subtle_link=1" >> $2.pref
echo "pf_cleandisplay=0" >> $2.pref
echo "sort=2" >> $2.pref
the /root/temp.txt contain three element, the first is email address, the second is file name, the third is user name . they are separated by space character
if I disable the cat /root....... command, I must input one by one.
so I want to make the shell script to read the value from the file temp.txt
However, I can't find the error , can anyone help me ?
thx a lot
chrism01
03-03-2003, 02:24 PM
The cat cmd is causing the shell script to read the temp.txt (indirectly).
Obviously, if you comment it out, it won't.
What error are you talking about??
Using other progs eg cat, inside a shell script is perfectly normal & reasonable.
Shell is a bit of a glue language ie you can call various built in prog/utils eg cat, grep, sed, awk and any other prog, even progs you've written yourself, to process data.
It is possible to actually read from the file directly, using pure shell cmds, but it'd be less efficient / overkill for what you're doing.
HTH
chris27
03-04-2003, 02:34 AM
Lawes,
Im not sure how awk works very well but I think what MIGHT be causing your error is that you are not properly assigning your variables when you try to append >> $2.pref
As I understand, $1,$2,and$3 read off the command line as arguments to the script name. I see that you are using the statement:
awk '{print $1 $2 $3}'
As I understand it, that just prints the first three fields of the contents of the file /root/temp.txt but it DOES NOT assign those values printed out as $1, $2 and $3. You need to directly assign those values to the variables that you are using to append with.
$1 $2 and $3 are read only so im not sure but something like
UserName=$(awk '{print $1}')
File=$(awk '{print $2})
Email=$(awk '{print $3})
might be what you are looking for.
anyway, thats just an example, I never get code right when I wing it:) But that is my 2 cents on your problem.
Lawes
03-04-2003, 02:35 AM
THX for your help
the problem I face is , if I comment the cat ...... command ,
I must input as follow one by one :
./shell_script_name email_address file_name user_name
the source is from the temp.txt file
so I want the script can automatic to get the raw data from the file temp.txt
but after I uncomment the cat ..... command
it can't work
bigrigdriver
03-04-2003, 04:09 AM
#cat /root/temp.txt | awk '{ print $1 $2 $3 }'
The problem may be the single quotes around {print $1 $2 $3}. Change those to double quotes "{print $1 $2 $3}". Reason: single quotes prevent the shell from substituting the value of a variable (source: A Practical Guide to Linux - Mark G. Sobell, page 320, last paragraph).
Lawes
03-04-2003, 06:43 AM
thx again for the reply
but I still suffer by the problem, I have the idea , but don't have the technology to apply on the script
let say, the temp.txt contain the info as follow
amy@test.com fileamy amy
tom@test.com filetom tom
john@test.com filejohn file
then if I can control the script to read it row by row, let say, the variable $1, $2, $3 is assign to the first field, second filed and third field automatically , it may be success.
the problem of cat /root/temp.txt | awk "{print $1 $2 $3}" is, it will show up all the context of the temp.txt file first, then apply the echo command later. therfore , the $1 $2 $3 get nothing input.
please help me ...... :(
bigrigdriver
03-04-2003, 02:13 PM
I have copied your script, and other information you gave. When I execute the script, it is writing to a file called .pref (note that is dot pref) in the home directory. I can't figure out how to make the script read the $2 parameter correctly to write to a file with the <username>.pref name.
Sorry I can't be of more help.
bwkaz
03-04-2003, 02:49 PM
OK, guys. There are two different $1, $2, $3, etc. sets of variables here.
In a normal shell script, you are all right, that you need double quotes or whatever for bash to substitute the command line args for the $1, $2, etc.
HOWEVER, that's not what Lawes is trying to do. ;) In awk, $1, $2, etc. are the fields in the line that awk is currently working on. So you need single quotes around them in order for awk to see them as $1 and not (for example) the first argument you pass to the script. For example, this:
#!/bin/bash
echo $1
echo "this is a test" | awk '{ print $1; }'
echo "this is a test" | awk "{ print $1; }" when executed like ./test hello, will print "hello" on one line (since that's what the value of bash's $1 is), then "this" on the next line (because that's what's in the first field coming into the awk command is), then "hello" again on the third line (because awk doesn't see $1 in that case; bash substitutes hello in instead).
SO....
I think the problem is a misunderstanding. awk will not assign anything to any variables outside its execution. You cannot modify the current shell's environment from inside awk.
So you can't assign to bash's $2 from what awk prints. You have to do something like what Chris27 posted -- do a filename=$(echo temp.txt | awk '{print $2; }' to get the filename into a variable named filename. Then use $filename.pref instead of $2.pref.
chrism01
03-04-2003, 03:53 PM
Here's a short bash soln for a file containing 3 fields separated by whitespace for each record
for t in `awk '{print $1"|"$2"|"$3 }' t.t`;
do echo $t;
done
t.t file is:
test for FAT write
2nd line test
output is
test|for|FAT
2nd|line|test
you could use the following to get each field:
for t in `awk '{print $1"|"$2"|"$3 }' t.t`
do
t1=`echo $t|cut -f1 -d'|'`
echo $t1
t2=`echo $t|cut -f2 -d'|'`
echo $t2
t3=`echo $t|cut -f3 -d'|'`
echo $t3
done
Lawes
03-06-2003, 05:08 AM
Dear All ,
thx very much for the help. Now I can create the necessary files.
Really thx for your hand.
:)