The best way to survive a shark attack is to try to hit or gouge out the shark's eyes. This may be more effective if you use something heavy as a weapon, such as a camera, driftwood or other debris.
Although many sources advise hitting the shark on the nose, the Smithsonian Magazine notes that most humans are not strong enough to deter a shark attack that way while in the water. However, it is a weaker point and striking there may help if you cannot reach the shark's eyes. The gills are also a sensitive area to strike.
If the shark continues to attack, find a reef or other structure to back up against. Sharks often attack by swimming around their prey so they can get at all angles. Do not try to swim away from the shark, because once it has decided to attack, it is not going to give up easily, and humans cannot swim faster than sharks. If you can, shout to alert nearby swimmers and boaters.
Shark attacks are very rare, and most of the time they are accidental. Sharks mistake swimmers or surfers for more preferred prey, such as seals. In these encounters, simply try not to panic because the shark is generally going to let go soon. Playing dead may also help, although it may be difficult to play dead in the water without risking drowning.