Link to home
Start Free TrialLog in
Avatar of Devoin Ruffin
Devoin Ruffin

asked on

Adding bash code to add data to a CSV file

Looking for Bash code to create csv file and add data into. I

1.) check to see if the file exist
2.) create file if does not exist.
3.) place code in the error handling to add data about the error from the current jobs that just ran.
Avatar of Travis Martinez
Travis Martinez
Flag of United States of America image

I'm not sure about the error handling portion because I don't know what the job is you're running; however, the following will test for the existence of the file and create it if it doesn't...

#!/bin/bash
if [ -e /path/test.txt ]
  then
     echo "File Exists"
     exit 0
  else
     touch /path/test.txt
fi
Avatar of Devoin Ruffin
Devoin Ruffin

ASKER

Thank let me try this and I will get back o you
I am getting a error when I place the code into a .sh file. I have attached the code to post. getting error on line 10 which does not exist
There may be some special characters from copy and paste.  I've put it in the "code" portion of the post.  Your attachment didn't get posted.

#!/bin/bash
if [ -e /path/test.txt ]
then
echo "File Exists"
exit 0
else
touch /path/test.txt
fi

Open in new window

Because it has the sh file ext I will try this new code and see what happen
I am still receiving a error with this script. I have attached a text file so you can review the code
forgot to attached the file
DevoinTEst2.txt
You need to replace the "/path/test.txt" with the actual path name and file name.  I've tested this with the path my home directory and it works as expected.  It's throwing an error at "line 10"?  Does it give anything further than that?
yes, I am replacing the path with my path, but it still give me a error syntax error near unexpected token `fi'
Okay, I've seen that before and it's a problem with the "else" portion but I can't remember what the fix was.  I'm researching.

In the mean time:

Please run the script like this:

bash -x file.sh where file.sh is your script file and provide the output.
ok
I just ran it using bash -x and the error is below - syntax error near unexpected token `fi'
It is the "else" statement.  We can accomplish the same thing by taking it out and leaving the exit 0 in but put the touch outside the if statement.

#!/bin/bash
if [ -e /path/test.txt ]
then
echo "File Exists"
exit 0
fi
touch /path/test.txt

Open in new window

still receiving the same error
syntax error: unexpected end of file line 8
Wow, okay.  Lets try a line terminator and see if that works.

#!/bin/bash
if [ -e /path/test.txt ];
then
echo "File Exists"
exit 0
else
touch /path/test.txt
fi

Open in new window


The addition was a semicolon at the bracket of the "if" statement.

If the above doesn't work, take the else statement out but leave the line terminator in.  It's working for me so I'm having difficulty troubleshooting why it isn't for you.
Thank I will try this again, but I have to leave soon for a appointment. When can we start this process up again this does not work.
It still not working, I will step back and think about what is going on. I will be working tomorrow, if you have time if not let me know when
I've really no idea why.  What version and brand of Linux are you running this on?  Have you built the script using just the vi editor and not copy and paste?

I'll be looking at email and will help if I can but I am stumped because all the different versions of the script I've given you.  There is another where "then" is at the test line but that shouldn't make a difference.
ok,  I will step back and think about tit and try again tomorrow
I will let you know if I get it to work
You should also try double brackets around the if test [[ -e /path/file ]]

Just thought of that.
Hi Devion,
I have a few questions for you.

Q1. Is this for homework?  If not, what is it for, please?

Q2. What exactly do you mean by this:
  "3.) place code in the error handling to add data about the error from the current jobs that just ran."
If you could include some examples to show what you expect the script to do in this respect, that could help.

Q3. Is there any reason why you don't replace the whole script with just this?:
      #!/bin/bash
      touch /path/test.txt
touch creates the file only if it doesn't exist already.
The only disadvantage I can think of is, this will change a timestamp on the file if it already exists.  Does that matter to you?  If so, you could make it so it changes the time-last-accessed only, like this:
      touch -a /path/test.txt
tel2, No this is for a company and I think I know what the problem is. It has to be the Bash profiler for my user name. I remember this about a year ago. do you know the code offhand for creating the bash profiler?
I've never heard of a bash profiler, sorry Devoin.  Are you sure you've got the right terminology?  When I Google it, the only profiling I see in the first hits is about bash performance profiling, i.e. measuring their speed of execution, etc.  What do you mean by "bash profiler"?

Also, what was the answer to questions Q2 & Q3 from my previous post?

BTW, this code Travis gave you:
    #!/bin/bash
    if [ -e test.txt ]
    then
    echo "File Exists"
    exit 0
    else
    touch test.txt
    fi
runs fine on the 2 Linux boxes I use (CentOS & OpenBSD).

Here's a more concise version which should basically do the same thing:
    #!/bin/bash
    [[ -e test.txt ]] && echo "File Exists" && exit
    touch test.txt
but I still suggest you consider the even shorter version from Q3 of my previous post, which doesn't even (explicitly) test for file existence.
If I remove the newline character from after the "fi" on the last line of Travis's code, I get this error, which is similar to the error you're getting:
    ./travis.sh: line 9: syntax error: unexpected end of file
Strangely, there is no line 9 in that code, as you can see:
#!/bin/bash
if [ -e test.txt ]
then
echo "File Exists"
exit 0
else
touch test.txt
if

Open in new window

What do you get if you run "cat -vet your_script_name.sh", Devion?  If no "$" shows after the "fi" in line 8, add a newline to your script.  One way to add a newline one is:
    echo >>your_script_name.sh
Then run the "cat -vet your_script_name.sh" command again, to check the "$" shows.

And please respond to my previous post.
apparently, you just want to add a line of data in the file after each job run

you probably can simply
echo "whatever,you,need" >> file

the file will be appended to and created if it does not exist previously

if you want better error handling, you can use
echo "whatever,you,need" | tee -a file
which will additionally return and outut an error if the file cannot be written to


@tel2:
on the 2 Linux boxes I use (CentOS & OpenBSD).
please folks BSD boxes are NOT linux in any way
Hi skullnobrains,

> "apparently, you just want to add a line of data in the file after each job run"
Yes, it looks as if that might be what Devion is actually needing here, and that simplifies the solution, as you have pointed out.  If that is what he meant, this looks like a classic XY question (Devion, please take note for the future).  I had considered that might be what he meant, and considered suggesting the first solution you did, but instead decided to ask exactly what he meant by "3.) place code in the error handling to add data about the error from the current jobs that just ran", because it wasn't clear to me that he's even talking about the CSV file when he says "in the error handling" here.  Still waiting for Devion's answer.

> "please folks BSD boxes are NOT linux in any way"
Correction accepted, I had forgotten that, although one way in which they are effectively the same is their ability to run simple bash scripts like the one in this question.
yeah. did not get the part regarding the "code in the error handling" either. just went with my own guess. ;)

no hard feelings regarding BSD. i'm just a little tired to see people lumping together very different things and the poor quality of many/most linux distribs casting shadows on the bsd world. btw, afaik, bash is not installed by default in any of the 3 major BSDs. and that's part of the difference in quality : nobody in the bsd world would write rc scripts using a buggy shell that never once in it's history ever released a version that did not introduce regressions and break backwards compatibility. it's just not safe.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.