first of all I thought the argument DONE is necessary for all scripts that have or begin with do statements which I have on my script, However, I still don't completely understand why I am receiving an error I tried adding another done argument statement but didn't do any good.
I appreciate some explanation or quick tips/suggestions...
Thanks!
Non-working, unable to finish/execute script below:
#[localuser@localhost ~]$ cat wk7.sh
1 #!/bin/bash
2
3 #Read tabc.txt
4 tabc="cat tabc.txt"
5
6 #Make week 7 assignment directory
7 wk7dir=~/"wk7dir"
8
9 #Prevents duplicates and errors
10 if [ ! -d $wk7dir ]; then
11 mkdir $wk7dir
12 fi
13
14 #Assign appropriate variables from tabc.txt
15 $tabc | sed -e 's/^\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\(.\{8\}\).*$/\3 \5 \6 \7 \8/' | while read fileuser filesize filedate filename; sed -e 's/^\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\(.\{8\}\).*$/\7/' tabc.txt | sed 's/://' | while read filetime; do
16
17 #Change filesize
18 dd if=/dev/zero of=$wk7dir/$filename bs=$filesize count=1
19
20 #Create files based on date and filename
21 touch -d $filedate $wk7dir/$filename
22
23 #Changes file permissions to allow read, write, and execute to all users
24 find ~/"wk7dir" -type f -exec chmod 755 {} \;;
25
26 ls -lR --time-style="+%Y%m%d" "wk7dir" | sed -e 's/^\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\)/\6/' | while read filedatestamp
27
28 #Modify file time
29 touch -mt $filedatestamp$filetime $wk7dir/$filename
30
31 done
~ ===
:(::SEE ERROR OUTPUT BELOW:::wall:
[localuser@localhost ~]$ ./wk7.sh
./wk7.sh: line 31: syntax error near unexpected token `done'
./wk7.sh: line 31: `done'
There is (at least) one syntax error in your script. Look at the following, quoted from your post:
#[localuser@localhost ~]$ cat wk7.sh
!/bin/bash
#Read tabc.txt
tabc="cat tabc.txt"
wk7dir=~/"wk7dir"
#Prevents duplicates and errors
if [ ! -d $wk7dir ]; then
mkdir $wk7dir
fi
#Assign appropriate variables from tabc.txt
$tabc | sed -e '<statement>' |\
while read fileuser filesize filedate filename;
sed -e '<statement' tabc.txt |\
sed 's/://' |\
while read filetime; do
#Change filesize
dd if=/dev/zero of=$wk7dir/$filename bs=$filesize count=1
#Create files based on date and filename
touch -d $filedate $wk7dir/$filename
#Changes file permissions to allow read, write, and execute to all users
find ~/"wk7dir" -type f -exec chmod 755 {} \;
ls -lR --time-style="+%Y%m%d" "wk7dir" |\
sed -e '<statement>' |\
while read filedatestamp
#Modify file time
touch -mt $filedatestamp$filetime $wk7dir/$filename
done
After the first and the third line the "do" is missing and some while-loops are not closed.
It pays usually to break up lines and indent properly. Most problems will become easier to track this way.
pretty much I did the indentation and close the while-loops using the "\" forward slash However, I still get the same error?
[localuser@localhost ~]$ ./wk7.sh
./wk7.sh: line 37: syntax error near unexpected token `done'
./wk7.sh: line 37: `done'
and you mentioned I am missing do argument after the first and third line? may you correct or point out which line or where it is exactly since I've done some revisions yet still has an error...
1 #!/bin/bash #INSERT DO ARGUMENT HERE???
2
3 #Read tabc.txt #INSERT DO ARGUMENT HERE???
4 tabc="cat tabc.txt"
5
6 #Make week 7 assignment directory
7 wk7dir=~/"wk7dir"
8
9 #Prevents duplicates and errors
10 if [ ! -d $wk7dir ]; then
11 mkdir $wk7dir
12 fi
13
14 #Assign appropriate variables from tabc.txt
15 $tabc | sed -e 's/^\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\(.\{8\}\).*$/\3 \5 \6 \7 \8/' |\
16 while read fileuser filesize filedate filename;
17 $tabc | sed -e 's/^\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\(.\{8\}\).*$/\7/' |\
18 sed 's/://'|\
19 while read filetime; do
20
21 #Change filesize
22 dd if=/dev/zero of=$wk7dir/$filename bs=$filesize count=1
23
24 #Create files based on date and filename
25 touch -d $filedate $wk7dir/$filename
26
27 #Changes file permissions to allow read, write, and execute to all users
28 find ~/"wk7dir" -type f -exec chmod 755 {} \;
29
30 ls -lR --time-style="+%Y%m%d" "wk7dir" |\
31 sed -e 's/^\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\)/\6/' |\
32 while read filedatestamp
33
34 #Modify file time
35 touch -mt $filedatestamp$filetime $wk7dir/$filename
36
37 done
~
~
~
~
~
~
~
~
~
~
~
:set number
---------- Post updated at 12:54 AM ---------- Previous update was at 12:48 AM ----------
Moreover, I am suspecting if I can have two while read statements?
the reason I have two is that I am trying to change field 7 "04:55" to not display or include the semi-colon...such a long hassle code but that's all I can think of to use sed strictly for this script the code I entered just to do that function is shown below:
sed -e 's/^\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\(.\{8\}\).*$/\7/' tabc.txt | sed 's/://' | while read filetime
First off, i doubt that this is a good usage of sed. It is generally better to work with shell built-ins and to an amazing extent this is possible once you think about it.
Sorry, my bad. I meant first and third "while-loop", not "line".
As far as your revised code goes there are still some loops not written properly. I have marked the places below:
#!/bin/bash #INSERT DO ARGUMENT HERE???
2
3 #Read tabc.txt #INSERT DO ARGUMENT HERE???
4 tabc="cat tabc.txt"
5
6 #Make week 7 assignment directory
7 wk7dir=~/"wk7dir"
8
9 #Prevents duplicates and errors
10 if [ ! -d $wk7dir ]; then
11 mkdir $wk7dir
12 fi
13
14 #Assign appropriate variables from tabc.txt
15 $tabc | sed -e '<statement>' |\
16 while read fileuser filesize filedate filename ; do
17 $tabc | sed -e '<statement>' |\
18 sed 's/://'|\
19 while read filetime; do
20
21 #Change filesize
22 dd if=/dev/zero of=$wk7dir/$filename bs=$filesize count=1
23
24 #Create files based on date and filename
25 touch -d $filedate $wk7dir/$filename
26
27 #Changes file permissions to allow read, write, and execute to all users
28 find ~/"wk7dir" -type f -exec chmod 755 {} \;
29
30 ls -lR --time-style="+%Y%m%d" "wk7dir" |\
31 sed -e '<statement>' |\
32 while read filedatestamp ; do
33
34 #Modify file time
35 touch -mt $filedatestamp$filetime $wk7dir/$filename
36
37 done
38 done
39 done