I want to ask you for help with this problem, I have this script to detect files (I'm learning), but it doesn't detect the files and I can't see the error.
Can you guide me?
Thank you!
Folder where the script is located and executed:
home/jlia/scripts
Folder where the files are located:
home/jlia/data
Contains:
jane_contact_07292018.csv
jane_profile_07272018.doc
list.txt
list.txt
001 jane /data/jane_profile_07272018.doc
002 kwood /data/kwood_profile_04022017.doc
003 pchow /data/pcwow_profile_11042019.doc
004 janez /data/janez_ptofile_11042019.doc
005 jane /data/jane_pic_07282018.jpg
006 kwood /data/kwood_pic_04032017.jpg
007 pchow /data/pcwow_pic_05162019.jpg
008 jane /data/jane_contact_07292018.csv
009 kwood /data/kwood_contact_04042017.csv
010 pchow /data/pchow_contact_05172019.csv
#!/bin/env bash
archivos=$(grep "/jane_" ../data/list.txt | cut -d' ' -f 3);
for file2 in $archivos;
do
if [ -e "..${file2}" ];then
echo "exist ..${file2}"; else
echo "not existe ..${file2}";
fi
done
Result:
not existe ../data/jane_profile_07272018.doc
not existe ../data/jane_pic_07282018.jpg
not existe ../data/jane_contact_07292018.csv
In the home/jlia/data folder there is a text file and others, with the script I extract with grep the location of the files that I must verify if they exist and return "exist" as a result
Problem, it doesn't detect the files that exist in the script but when executing it independently [ -e "Expression" ] it detects it.
Something doesn't seem to jive between your script and your messaging.
If the files weren't where they should be, for the script to report correctly, then the setting of archivos using the grep and relative reference would fail!!!
The fact that archivos was correctly set with valid path values from list.txt, the logic should have worked.
I am unable to reproduce your results, even using the script and creating demo directory and dummy files.
My version of the script is this:
#!/bin/bash
cd ${HOME}/scripts
if [ ! -s ../data/list.txt ]
then
printf "\n WARNING: Creating DEMO Data ...\n"
mkdir ../data
cat >"../data/list.txt" <<"EnDoFiNpUt"
001 jane /data/jane_profile_07272018.doc
002 kwood /data/kwood_profile_04022017.doc
003 pchow /data/pcwow_profile_11042019.doc
004 janez /data/janez_ptofile_11042019.doc
005 jane /data/jane_pic_07282018.jpg
006 kwood /data/kwood_pic_04032017.jpg
007 pchow /data/pcwow_pic_05162019.jpg
008 jane /data/jane_contact_07292018.csv
009 kwood /data/kwood_contact_04042017.csv
010 pchow /data/pchow_contact_05172019.csv
EnDoFiNpUt
if [ ! -s ../data/jane_contact_07292018.csv ]
then
printf "\n WARNING: "
echo "Dummy Content CREATED - jane_contact_07292018.csv" | tee ../data/jane_contact_07292018.csv
fi
if [ ! -s ../data/jane_profile_07272018.doc ]
then
printf "\n WARNING: "
echo "Dummy Content CREATED - jane_profile_07272018.doc" | tee ../data/jane_profile_07272018.doc
fi
echo ""
fi
archivos=$(grep "/jane_" ../data/list.txt | cut -d' ' -f 3) ;
for file2 in $archivos
do
if [ -e "..${file2}" ]
then
echo "exist ..${file2}"
else
echo "not existe ..${file2}"
fi
done
The output generated is this:
WARNING: Creating DEMO Data ...
mkdir: cannot create directory ‘../data’: File exists
WARNING: Dummy Content CREATED - jane_contact_07292018.csv
WARNING: Dummy Content CREATED - jane_profile_07272018.doc
exist ../data/jane_profile_07272018.doc
not existe ../data/jane_pic_07282018.jpg
exist ../data/jane_contact_07292018.csv
Related
How to write a shell script that creates empty files with all possible permissions. File names should be, for example, rwxrw_r__.txt. I know how to do it manually. As an example:
#!/bin/bash
touch task5/rwxrwxrwx.txt | chmod 777 task5/rwxrwxrwx.txt
touch task5/rwxr-xr-x.txt | chmod 755 task5/rwxr-xr-x.txt
touch task5/rwx------.txt | chmod 700 task5/rwx------.txt
touch task5/rw-rw-rw-.txt | chmod 666 task5/rw-rw-rw-.txt
touch task5/rw-r--r--.txt | chmod 644 task5/rw-r--r--.txt
touch task5/rw-------.txt | chmod 600 task5/rw-------.txt
I do not know how to write a script that will create files according to the required template and give them permissions
Do a loop:
for ((i=0; i < 512; i++)); do
mod=$(printf "%03o" "$i");
touch ${mod}.txt; chmod $mod $mod.txt;
done
Rather than trying to construct the names, if you want the names to look like the output of ls -l, just do something like
for ((i=0; i < 512; i++)); do
mod=$(printf "%03o" "$i")
touch ${mod}.txt
chmod $mod $mod.txt
n=$(ls -l $mod.txt | cut -b1-10)
mv -- $mod.txt "$n.txt"
done
It's just a permutations problem.
p=( --- --x -w- -wx r-- r-x rw- rwx ) # the set of permissions
for u in "${p[#]}"; do for g in "${p[#]}"; do for o in "${p[#]}"; do
f="task5/$u$g$o.txt"; touch -- "$f" && chmod "u=${u//-/},g=${g//-/},o=${o//-/}" -- "$f";
done; done; done
NOTE
thanks to #kvantour for pointing out I was passing dashed to chmod, and that it doesn't know what to do with them. I am surprised I wasn't getting errors.
Let's break it down and look at what's happening.
If you have any questions about what permissions sets mean or how chmod works, see here.
So for each of the user, group, or other, there are eight possible symbolic representations (representing the values of one octal digit, 0-7).
We set those into a simple array we can loop over.
p=( --- --x -w- -wx r-- r-x rw- rwx ) # the set of permissions
You can access any element with it's octal digit (technically the decimal equivalent, but that doesn't matter unless you go over 7) so ${p[5]} is r-x. Indexing with # returns the whole array, so the loops walk through them sequentially with ${p[#]}.
To get every possible permutation, we loop over them for each of user/group/other.
for u in "${p[#]}"; do # assign each permission set for the user
for g in "${p[#]}"; do # assign each permission set for the group
for o in "${p[#]}"; do # assign each permission set for the other
This is just simple iterations in nested loops to hit every permutation.
f="task5/$u$g$o.txt" # assign the permissions string AS the filename
By putting the path and filename info into a variable, we can maintain any changes in one place, and it makes the rest of the line shorter and easier to read.
touch -- "$f" && # create the file and test for success
touch will create an empty file. Because the filenames could sometimes begin with a dash (any time the permissions disallow user read), we give touch a first argument of --, which is a *NIX standard idiom meaning "options are done now, anything left is arguments"; otherwise it would try to interpret a leading dash as an invalid option set and fail. This won't be a problem while you are putting "task5/" at the beginning of the filename, but if you end up using the filename bare it would.
The && is a boolean test to see whether touch succeeded. If it did not, then we silently skip trying the chmod (touch should have emitted an error message for your debugging, but if that fails, you probably got a ton of them, and will need to fix whatever ...)
chmod "u=${u//-/},g=${g//-/},o=${o//-/}" -- "$f" # change the permissions
This uses chmod's symbolic mode. We have the permissions of each section from the nexted loops - just apply them. Again, we use the -- to tell chmod when we are done passing options so that leading dashes in filenames won't be a problem later if you refactor just just cd into the directory and create the files locally, though even then you could always prefix ./ or $PWD/ on it.
We have to get rid of thew dashes in the symbolic file modes, though, as (thanks agains #kvantour) chmod doesn't recognize those. An inline string edit works beautirully: in "u=${u//-/},g=${g//-/},o=${o//-/}", the // inside the variable spec is a replacement of all occurrences, replacing - with the nothing between the following / and }.
done; done; done # these just close each of the loops
We could (and probably should) put each of these on separate lines, but the interpreter doesn't care since we used semicolons. It lets us compact the code to put the loop nesting and closures on lines together, so long as you are comfortable with the ONE thing that's changing being obvious enough.
Anything you still have questions about that I didn't cover?
Alternate
Another version, because I like going through a loop once instead of nested shenannigans...
p=( --- --x -w- -wx r-- r-x rw- rwx ) # the set of permissions
for dec in {0..511}; do oct="$(printf "%03o" "$dec")"
u="${p[${oct:0:1}]}"; g="${p[${oct:1:1}]}"; o="${p[${oct:2:1}]}";
f="task5/$u$g$o.txt"; touch "$f"; chmod "u=${u//-/},g=${g//-/},o=${o//-/}" "$f";
done
This walks through the combinations numerically, converts decimal to octal with printf, slices each digit out of the octal permission set with basic substring parsing and uses it to look up the relevant string from the array, assign the segments, assign the resulting filename, touch/create the file, then apply the scrubbed permissions strings with chmod. It's once-through and faster, if a little harder to understand.
u="${p[${oct:0:1}]}" # grabs 1 byte from offset 0 of $oct as index to $p
As suggested, to skip the decimal to octal conversion step, replace:
for dec in {0..511}; do oct="$(printf "%03o" "$dec")"
with
for oct in {0..7}{0..7}{0..7}; do
As per Paul Hodges answer - this is a permutation question - . However, this approach uses bash with SQLite in memory database to generate a data set inspired by this web page, https://towardsdatascience.com/unix-permissions-the-easy-way-98cc19979b3e creating a cartesian product out of eight records - generating the commands and placing the octal alongside the user group all permission set
The octal for the chmod is illustrated in this approach - which more aligns with the original request
#!/bin/bash
WorkingFolder=task5
RunnerFile=_T5Builder.bash
rm -Rf ${WorkingFolder} ${RunnerFile}
mkdir ${WorkingFolder}
sqlite3 << FIN
.headers off
.mode column
.once ${RunnerFile}
with cte_elements as (
select '0' as Id, '000' as Octal, '---' as Permission union
select '1', '001', '--x' union select '2', '010', '-w-' union
select '3', '011', '-wx' union select '4', '100', 'r--' union
select '5', '101', 'r-x' union select '6', '110', 'rw-' union
select '7', '111', 'rwx'
)
, cte_allpermissions as (
select cte_elements.Id || cte_elements1.Id || cte_elements2.Id as Dec
, cte_elements.Octal || cte_elements1.Octal || cte_elements2.Octal as OctFull
, cte_elements.Permission || cte_elements1.Permission || cte_elements2.Permission as Permission
from -- cartesian product
cte_elements cross join cte_elements cte_elements1 cross join cte_elements cte_elements2 --cartesian product
)
, cte_constructor as (
select 'touch ${WorkingFolder}/' || Dec || '_' || Permission || '.txt;' as "#TouchCommand"
, 'chmod ' || Dec || ' ${WorkingFolder}/' || Dec || '_' || Permission || '.txt ;' as ChmodCommand
, 'echo -n "' || Dec || ' "' as Progress
from cte_allpermissions
)
select * from cte_constructor;
FIN
chmod +x ${RunnerFile}
echo Top And Tail of Runner File ${RunnerFile}
head -2 ${RunnerFile}
tail -2 ${RunnerFile}
source ${RunnerFile}
echo
echo Done
ls -l ${WorkingFolder} | awk '{print $1, $9}' | more
Output as follows (takes about 40 seconds - if performance is really an issue
MyNode:~/viasql$ time ./CreateAllPermFiles.bash
Top And Tail of Runner File _T5Builder.bash
touch task5/000_---------.txt; chmod 000 task5/000_---------.txt ; echo -n "000 "
touch task5/001_--------x.txt; chmod 001 task5/001_--------x.txt ; echo -n "001 "
touch task5/776_rwxrwxrw-.txt; chmod 776 task5/776_rwxrwxrw-.txt ; echo -n "776 "
touch task5/777_rwxrwxrwx.txt; chmod 777 task5/777_rwxrwxrwx.txt ; echo -n "777 "
000 001 002 003 004 005 006 007 010 011 012 013 014 015 016 017 020 021 022 023 024 025 026 027 030 031 032 033 034 035 036 037 040 041 042 043 044 045 046 047 050 051 052 053 054 055 056 057 060 061 062 063 064 065 066 067 070 071 072 073 074 075 076 ...
First few files
... 761 762 763 764 765 766 767 770 771 772 773 774 775 776 777
Done
total
---------- 000_---------.txt
---------x 001_--------x.txt
--------w- 002_-------w-.txt
--------wx 003_-------wx.txt
-------r-- 004_------r--.txt
-------r-x 005_------r-x.txt
-------rw- 006_------rw-.txt
-------rwx 007_------rwx.txt
------x--- 010_-----x---.txt
------x--x 011_-----x--x.txt
------x-w- 012_-----x-w-.txt
How to write a shell script that creates empty files with all possible permissions. File names should be, for example, rwxrw_r__.txt. I know how to do it manually. As an example:
#!/bin/bash
touch task5/rwxrwxrwx.txt | chmod 777 task5/rwxrwxrwx.txt
touch task5/rwxr-xr-x.txt | chmod 755 task5/rwxr-xr-x.txt
touch task5/rwx------.txt | chmod 700 task5/rwx------.txt
touch task5/rw-rw-rw-.txt | chmod 666 task5/rw-rw-rw-.txt
touch task5/rw-r--r--.txt | chmod 644 task5/rw-r--r--.txt
touch task5/rw-------.txt | chmod 600 task5/rw-------.txt
I do not know how to write a script that will create files according to the required template and give them permissions
Do a loop:
for ((i=0; i < 512; i++)); do
mod=$(printf "%03o" "$i");
touch ${mod}.txt; chmod $mod $mod.txt;
done
Rather than trying to construct the names, if you want the names to look like the output of ls -l, just do something like
for ((i=0; i < 512; i++)); do
mod=$(printf "%03o" "$i")
touch ${mod}.txt
chmod $mod $mod.txt
n=$(ls -l $mod.txt | cut -b1-10)
mv -- $mod.txt "$n.txt"
done
It's just a permutations problem.
p=( --- --x -w- -wx r-- r-x rw- rwx ) # the set of permissions
for u in "${p[#]}"; do for g in "${p[#]}"; do for o in "${p[#]}"; do
f="task5/$u$g$o.txt"; touch -- "$f" && chmod "u=${u//-/},g=${g//-/},o=${o//-/}" -- "$f";
done; done; done
NOTE
thanks to #kvantour for pointing out I was passing dashed to chmod, and that it doesn't know what to do with them. I am surprised I wasn't getting errors.
Let's break it down and look at what's happening.
If you have any questions about what permissions sets mean or how chmod works, see here.
So for each of the user, group, or other, there are eight possible symbolic representations (representing the values of one octal digit, 0-7).
We set those into a simple array we can loop over.
p=( --- --x -w- -wx r-- r-x rw- rwx ) # the set of permissions
You can access any element with it's octal digit (technically the decimal equivalent, but that doesn't matter unless you go over 7) so ${p[5]} is r-x. Indexing with # returns the whole array, so the loops walk through them sequentially with ${p[#]}.
To get every possible permutation, we loop over them for each of user/group/other.
for u in "${p[#]}"; do # assign each permission set for the user
for g in "${p[#]}"; do # assign each permission set for the group
for o in "${p[#]}"; do # assign each permission set for the other
This is just simple iterations in nested loops to hit every permutation.
f="task5/$u$g$o.txt" # assign the permissions string AS the filename
By putting the path and filename info into a variable, we can maintain any changes in one place, and it makes the rest of the line shorter and easier to read.
touch -- "$f" && # create the file and test for success
touch will create an empty file. Because the filenames could sometimes begin with a dash (any time the permissions disallow user read), we give touch a first argument of --, which is a *NIX standard idiom meaning "options are done now, anything left is arguments"; otherwise it would try to interpret a leading dash as an invalid option set and fail. This won't be a problem while you are putting "task5/" at the beginning of the filename, but if you end up using the filename bare it would.
The && is a boolean test to see whether touch succeeded. If it did not, then we silently skip trying the chmod (touch should have emitted an error message for your debugging, but if that fails, you probably got a ton of them, and will need to fix whatever ...)
chmod "u=${u//-/},g=${g//-/},o=${o//-/}" -- "$f" # change the permissions
This uses chmod's symbolic mode. We have the permissions of each section from the nexted loops - just apply them. Again, we use the -- to tell chmod when we are done passing options so that leading dashes in filenames won't be a problem later if you refactor just just cd into the directory and create the files locally, though even then you could always prefix ./ or $PWD/ on it.
We have to get rid of thew dashes in the symbolic file modes, though, as (thanks agains #kvantour) chmod doesn't recognize those. An inline string edit works beautirully: in "u=${u//-/},g=${g//-/},o=${o//-/}", the // inside the variable spec is a replacement of all occurrences, replacing - with the nothing between the following / and }.
done; done; done # these just close each of the loops
We could (and probably should) put each of these on separate lines, but the interpreter doesn't care since we used semicolons. It lets us compact the code to put the loop nesting and closures on lines together, so long as you are comfortable with the ONE thing that's changing being obvious enough.
Anything you still have questions about that I didn't cover?
Alternate
Another version, because I like going through a loop once instead of nested shenannigans...
p=( --- --x -w- -wx r-- r-x rw- rwx ) # the set of permissions
for dec in {0..511}; do oct="$(printf "%03o" "$dec")"
u="${p[${oct:0:1}]}"; g="${p[${oct:1:1}]}"; o="${p[${oct:2:1}]}";
f="task5/$u$g$o.txt"; touch "$f"; chmod "u=${u//-/},g=${g//-/},o=${o//-/}" "$f";
done
This walks through the combinations numerically, converts decimal to octal with printf, slices each digit out of the octal permission set with basic substring parsing and uses it to look up the relevant string from the array, assign the segments, assign the resulting filename, touch/create the file, then apply the scrubbed permissions strings with chmod. It's once-through and faster, if a little harder to understand.
u="${p[${oct:0:1}]}" # grabs 1 byte from offset 0 of $oct as index to $p
As suggested, to skip the decimal to octal conversion step, replace:
for dec in {0..511}; do oct="$(printf "%03o" "$dec")"
with
for oct in {0..7}{0..7}{0..7}; do
As per Paul Hodges answer - this is a permutation question - . However, this approach uses bash with SQLite in memory database to generate a data set inspired by this web page, https://towardsdatascience.com/unix-permissions-the-easy-way-98cc19979b3e creating a cartesian product out of eight records - generating the commands and placing the octal alongside the user group all permission set
The octal for the chmod is illustrated in this approach - which more aligns with the original request
#!/bin/bash
WorkingFolder=task5
RunnerFile=_T5Builder.bash
rm -Rf ${WorkingFolder} ${RunnerFile}
mkdir ${WorkingFolder}
sqlite3 << FIN
.headers off
.mode column
.once ${RunnerFile}
with cte_elements as (
select '0' as Id, '000' as Octal, '---' as Permission union
select '1', '001', '--x' union select '2', '010', '-w-' union
select '3', '011', '-wx' union select '4', '100', 'r--' union
select '5', '101', 'r-x' union select '6', '110', 'rw-' union
select '7', '111', 'rwx'
)
, cte_allpermissions as (
select cte_elements.Id || cte_elements1.Id || cte_elements2.Id as Dec
, cte_elements.Octal || cte_elements1.Octal || cte_elements2.Octal as OctFull
, cte_elements.Permission || cte_elements1.Permission || cte_elements2.Permission as Permission
from -- cartesian product
cte_elements cross join cte_elements cte_elements1 cross join cte_elements cte_elements2 --cartesian product
)
, cte_constructor as (
select 'touch ${WorkingFolder}/' || Dec || '_' || Permission || '.txt;' as "#TouchCommand"
, 'chmod ' || Dec || ' ${WorkingFolder}/' || Dec || '_' || Permission || '.txt ;' as ChmodCommand
, 'echo -n "' || Dec || ' "' as Progress
from cte_allpermissions
)
select * from cte_constructor;
FIN
chmod +x ${RunnerFile}
echo Top And Tail of Runner File ${RunnerFile}
head -2 ${RunnerFile}
tail -2 ${RunnerFile}
source ${RunnerFile}
echo
echo Done
ls -l ${WorkingFolder} | awk '{print $1, $9}' | more
Output as follows (takes about 40 seconds - if performance is really an issue
MyNode:~/viasql$ time ./CreateAllPermFiles.bash
Top And Tail of Runner File _T5Builder.bash
touch task5/000_---------.txt; chmod 000 task5/000_---------.txt ; echo -n "000 "
touch task5/001_--------x.txt; chmod 001 task5/001_--------x.txt ; echo -n "001 "
touch task5/776_rwxrwxrw-.txt; chmod 776 task5/776_rwxrwxrw-.txt ; echo -n "776 "
touch task5/777_rwxrwxrwx.txt; chmod 777 task5/777_rwxrwxrwx.txt ; echo -n "777 "
000 001 002 003 004 005 006 007 010 011 012 013 014 015 016 017 020 021 022 023 024 025 026 027 030 031 032 033 034 035 036 037 040 041 042 043 044 045 046 047 050 051 052 053 054 055 056 057 060 061 062 063 064 065 066 067 070 071 072 073 074 075 076 ...
First few files
... 761 762 763 764 765 766 767 770 771 772 773 774 775 776 777
Done
total
---------- 000_---------.txt
---------x 001_--------x.txt
--------w- 002_-------w-.txt
--------wx 003_-------wx.txt
-------r-- 004_------r--.txt
-------r-x 005_------r-x.txt
-------rw- 006_------rw-.txt
-------rwx 007_------rwx.txt
------x--- 010_-----x---.txt
------x--x 011_-----x--x.txt
------x-w- 012_-----x-w-.txt
I'm trying to make a shell script that searches some lines in .log file.
I need to first search the number and place them into a file, then search in that file for each ID and store them in array in order to search again in file for the responses.
Code:
#!/bin/bash
logFile='/path/to/file.log'
if [[ $1 = "" ]]; then
echo "Use ./analize.sh <number>";
exit 0
fi
result=rs-$1.log;
touch result;
grep -B 7 -A 1 $1 $logFile >> result;
IDarr[i]=($(cat result | grep 'ID:'))
while [ -n "$IDarr[i]" ]
do grep -B 2 -A 6 ${IDarr[n]} $result >> idmatched.log
done
exit 0
Thanks
logfile example:
017-06-13 12:00:32 - Outbount Request
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
ID: 132
Request: [FW1;14dwa90266681;12eqw28631898;99514;321;634579;224943746;2]
Encription: YES
Export-Value: 2048
Type: String
Content: {Status=[Failed], Target={"number":"9892213994","asID":"a321kpok41hadw-0-391-12391421.00","version":"09e2da8d-7379-39d9-9c52-be4aba0d49d1", t-qgs-ms=[4], java 1.7, h-ucc-dn=[***************************]}, accept-encoding=[identity], Content-Length=[300],}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
2017-06-13 12:10:51 - Inbound Response
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
ID: 132
Response-Code: 200
Encription: YES
Export-Value: 2048
Headers: {Content-Length=[39], charset=UTF-8], Date=[13 Jun 2017 12:10:51 GMT]}
Load: {"result":"E9W2KE3MNA","Message":"Successful"}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
My script
#!/bin/bash
cp *.ats /home/milenko/procmt
mycd() {
cd /home/milenko/procmt
}
mycd
EXT=ats
for i in *; do
if [ "${i}" != "${i%.${EXT}}" ];then
./tsmp -ascii i
fi
done
But
milenko#milenko-HP-Compaq-6830s:~/Serra do Mel/MT06/meas_2016-07-13_20-22-00$ bash k1.sh
./tsmp: handling 1 files ************************************** total input channels: 1
the name of your file does not end with ats ... might crash soon
main (no rda) -> can not open i for input, exit
./tsmp: handling 1 files ************************************** total input channels: 1
the name of your file does not end with ats ... might crash soon
main (no rda) -> can not open i for input, exit
When I go to procmt directory and list files
milenko#milenko-HP-Compaq-6830s:~/procmt$ ls *.ats
262_V01_C00_R000_TEx_BL_2048H.ats 262_V01_C00_R086_TEx_BL_4096H.ats 262_V01_C02_R000_THx_BL_2048H.ats
262_V01_C00_R000_TEx_BL_4096H.ats 262_V01_C01_R000_TEy_BL_2048H.ats 262_V01_C03_R000_THy_BL_2048H.ats
What is wrong with my script?
If I understand correctly this should work for you:
dest='/home/milenko/procmt'
cp *.ats "$dest"
cd "$dest"
for i in *.ats; do
./tsmp -ascii "$i"
done
There is no need to loop through all files when you're only interested in .ats files. Your mycd function is just doing cd so you can avoid that as well.
I have a linux server running, which I back up every day.
The backup script works like a charm, but I would like some feedback on completion with or without errors.
#!/bin/bash
# Hier gaan we zoeken naar alle mappen en onderliggende mappen in de LinuxKJ server.
find /var/www -maxdepth 1 -mindepth 1 -print0 | while read -d $'\0' foldername
do
# Hier gaan we alle mappen in zipjes comprimeren en overzetten naar een andere locatie. Overigens laat hij _Inactief eruit.
fatsoenlijkPad=$(echo $foldername)
tar --exclude='/var/www/_Inactief' -zcvpf /mediabak/winboxbak/"${fatsoenlijkPad##*/}".tar.gz "$fatsoenlijkPad"
# Hier gaan we kijken of de functie hierboven een succes return (Succes = 0) (Fout = 1,2,3,4, etc)
if ( $? == 0 ) then
Mailtext=$(echo "Backup succesvol.")
else
Mailtext=$(echo "Backup failed.")
fi
done
# Hier gaan we mailen wat de functie heeft gereturned
mail -s "Linux backup" "example#example.com"
$Mailtext
./backupscript.sh: line 9: 141: command not found
Can anyone help me to fix this issue?
In bash square brackets are used. Hence change
if ( $? == 0 ) then
to
if [ $? == 0 ]; then
Edit: Change
mail -s "Linux backup" "example#example.com"
$Mailtext
to
echo $Mailtext | mail -s "Linux backup" example#example.com
To verify that your are able to send and receive a mail, try to send a mail with dummy text as below.
echo "Testing Mail" | mail -s "Linux backup" example#example.com
As commented, your variable assignement for Mailtext are inefficient (it works, but it has no sense to use an echo command to assign a text value).
As for your email sending, your mail command invocation should be :
echo $Mailtext | mail -s "Linux backup" "example#example.com"