I've been up close with grizzly and black bears before, I've had them come sniffing through my camp and I've done nothing but lay quiet in my sleeping bag and wait for them to wander off, which they usually do. Most of the time I don't even know they were there, I just find their tracks the next morning.
Believe it or not bears spook really easily. I've never had to use bear spray or shoot at a bear, they always run off as soon as we let them know we're getting close. So if you're in your tent and a bear is taking his time investigating your site, all you have to do most of the time is just announce yourself and that should be enough to scare them away. The human voice is the number one deterrent for bears, so just start making loud noises.
This will work most of the time, but the real problem bears are the ones that are used to humans, especially the ones that have been fed by humans in the past or found food in camps before, if one of these bears wanders into you site and doesn't act like it intends to leave without first finding something to eat, then get your bear spray out, get out of the tent, act big and keep shouting. You need to be pretty close for bear spray to be effective (30ft/9m or closer) so only spray it if your other attempts don't work and the bear is approaching you, or letting you approach him. Do NOT spray it inside of your tent, you will immediately regret it and possibly do yourself some serious harm. Try to get upwind of the bear if possible, and aim for his nose. One good blast and that bear should take off real quick.
Most people only cary one or the other, bear spray or a gun. A warning shot from a gun does just as good as bear spray does. People around here (Canadian Rockies) that hike with shotguns load it with 3 different types of ammo; the first round is just bird shot, to shoot in the air as a warning shot; the second shot is a slug, to shoot past the bear so he can hear the ball whistling past him; the rest of your rounds are all hollow-point bear stoppers, to put the beast down, because if the first two shots don't scare him away then nothing else is going to stop him.
Hunters have bigger problems with bears than campers do, because hunters typically have a fresh kill with them that the bear wants (usually grizzly bears), with campers and hikers, it's usually a case of the hikers sneaking up on the bear unintentionally and surprising it-in which case the bear acts defensively- or bears happening upon a campsite because it's on the side of a trail (bears use trails too) and finding food there.
Shotguns are only necessary if the bear charges, tries to get into your tent, or is following you and you can't shake them off your trial. This is especially true with black bears, if a black bear is following you, and isn't scared off by any of your attempts to get him to leave, then odds are good you're going to have to either fight it off or shoot it, because there have been incidents where black bears have been known to stalk people for food. These incidents are very rare, and will typically only occur in the most wild areas where bears don't have much contact with humans.
... suppose a bear finds my tent and likes the smell of warm man-flesh
inside of it.
Coming from bear country, if shouting at it doesn't scare it away and you have a gun, ten out of ten people where I'm from would say shoot it. Right through the tent. Don't even feel guilty about it, because if you don't shoot it, then a ranger probably will as soon as you report the encounter (assuming you managed to scare it off some other way). They don't tolerate bears that behave that way because they're a risk to people's safety. You've probably heard the saying, "A fed bear is a dead bear." They say that because 99% of the time, bears that get hand outs or find
human food end up getting put down because they always come back for more.
If you have a bear trying to get in your tent, and you don't have a gun: FIGHT IT. You are literally fighting for your life, so kick, scream, hit it in the face with anything you've got. Bear spray will only work if there's nothing in between you and the bear, so try to get out of your tent before trying to spray it, or like I said already, you can actually make things worse for yourself if you spray it inside of your tent.
For Reference: To put it in perspective, deaths from black bear attacks in North America are about as common as shark attacks, and in many of the fatal encounters with black bears, the bear had fed on the victim.