temporary file to file then move to directory

Ok in my bash script i have 5 options to create a simple html script. I need to create a temporary file and whatever the user types will be stored in that file using html codes. And then I have another option in which that temporary file will be moved to the public_html directory in which the user gets to name the file like WEB.html.

Im not really sure how to create a temporary file and then move everything from the temporary file to a file.

This is what i have so far

create temp file

htmlcode=$(mktemp ~/tmp/htmlcode.XXXXXXXXXX)
ls > $htmlcode

and this is what i use to move the users input to the temporary file

echo "$text" >> ~/tmp/htmlcode.XXXXXXXXXX

but when i do this its creating a temp file and a file called htmlcode. It doesnt store in the temp file only in htmlcode?

Try

htmlcode=$(mktemp -t htmlcode.XXXXXXXXXX)
...
echo "$text" >> $htmlcode
1 Like

so this line is to create the temp file? Where is it being created?

htmlcode=$(mktemp -t htmlcode.XXXXXXXXXX)

and this is to forward the text into the temp file

echo "$text" >> $htmlcode

It's created in (probably) /tmp - check the man page.

yea its stored in temp, how would i cat the file like

cat ~/tmp/$WEB

?

Yes (but note that /tmp and ~/tmp are usually not the same thing).

it doesnt work, i tried to add the -> .XXXXXXXXX
didnt work

cat /tmp/$WEB

its option 2 in my loop, when i enter 2 nothing happens

edit i changed the ;; to break;;

but i get this error

cat: /tmp//tmp/WEB.DHiVwTnhVc: No such file or directory

What you're doing doesn't seem very clear, I'm afraid. Post the code.

EDIT: From your error, lose the /tmp/ on cat - $WEB already contains the full path.

...and the ENTIRE code, not just the part that errors out.

just cat $WEB doesnt work it still gives the error

cat: /tmp/WEB.i077x1v6mW: No such file or directory
2)
echo "Here is the code:"
cat /tmp/$WEB

break;;

cant get it to work, cat $WEB doesnt work either.

Post your

complete

script, not just option 2 which is broken.

there is nothing wrong with the script it something wrong with the temporary file part

WEB=$(mktemp -t WEB.XXXXXXXXXX)
cat $WEB

that is all, i cannot cat the $web variable to view its contents. Thats all im having trouble with

So in that case, does this work for you outside of your script? Just type this in the shell like I did and I did not get any error below:

sherlock ~]$ WEB=$(mktemp -t WEB.XXXXXXXXX)
sherlock ~]$ cat $WEB
sherlock ~]$ echo $WEB
/tmp/WEB.RvGpfFchV

yes that worked

but when i store something in a file and i do cat $WEB, shouldnt it show me the contents of that file?

Again, I don't know what your script looks like, so can't tell what is happening, so from the shell only and it works (I did an echo statement to the temp file and was able to cat the contents):

sherlock ~]$ WEB=$(mktemp -t WEB.XXXXXXXXX)
sherlock ~]$ echo "this is a test" >>$WEB
sherlock ~]$ cat $WEB
this is a test
sherlock ~]$ echo $WEB
/tmp/WEB.uIDFlfw6e

ok heres the script, CAT still doesnt work

while true
do
clear
echo "Menu Options: "
echo "1. Create Script"
echo "2. Show Script"
echo "3. Exit"
echo " Choose: "
read options
    
WEB=$(mktemp -t WEB.XXXXXXXXXX)

case "${options}" in
        


1)     read -p "Enter script name" $name
        echo "$name" >> $WEB 
        ;;
        
2)     echo "Your script"
        cat $WEB
        ;;

3)    echo "Exitting."
       exit 0            
        ;;

esac
done

Replace everywhere, the word WEB in your script with htmlcode and re-run it.
So $WEB Should read $htmlcode.

sorry that was a mistake i making changes, i finished editing it

Try this as it is and see if this works and then can customize as you want:

WEB=$(mktemp -t WEB.XXXXXXXXXX)
while true
do
echo "Menu Options: "
echo "1. Create Script"
echo "2. Show Script"
echo "3. Exit"
echo " Choose: "
read options
    

case "${options}" in
        


1)     read -p "Enter script name" name
        
        echo "$name" >> $WEB 
        ;;
        
2)     echo "Your script"
        
        cat $WEB
        ;;

3)    echo "Exitting."
       exit 0            
        ;;

esac
done

The issue is that you're creating a new (differently named) temp file every time through the while loop - you're echoing to one file and then catting a different file.

You need to check if it already exists, or create the temp file outside the loop (as in dude2cool's answer).

As demonstrated, where you think the problem may be is not always where it actually is :p.