Asked  7 Months ago    Answers:  5   Viewed   37 times

There are two ways to capture the output of command line in bash:

  1. Legacy Bourne shell backticks ``:

    var=`command`
    
  2. $() syntax (which as far as I know is Bash specific, or at least not supported by non-POSIX old shells like original Bourne)

    var=$(command)
    

Is there any benefit to using the second syntax compared to backticks? Or are the two fully 100% equivalent?

 Answers

44

The major one is the ability to nest them, commands within commands, without losing your sanity trying to figure out if some form of escaping will work on the backticks.

An example, though somewhat contrived:

deps=$(find /dir -name $(ls -1tr 201112[0-9][0-9]*.txt | tail -1l) -print)

which will give you a list of all files in the /dir directory tree which have the same name as the earliest dated text file from December 2011 (a).

Another example would be something like getting the name (not the full path) of the parent directory:

pax> cd /home/pax/xyzzy/plugh
pax> parent=$(basename $(dirname $PWD))
pax> echo $parent
xyzzy

(a) Now that specific command may not actually work, I haven't tested the functionality. So, if you vote me down for it, you've lost sight of the intent :-) It's meant just as an illustration as to how you can nest, not as a bug-free production-ready snippet.

Tuesday, June 1, 2021
 
Eugenie
answered 7 Months ago
36

SET XACT_ABORT ON instructs SQL Server to rollback the entire transaction and abort the batch when a run-time error occurs. It covers you in cases like a command timeout occurring on the client application rather than within SQL Server itself (which isn't covered by the default XACT_ABORT OFF setting.)

Since a query timeout will leave the transaction open, SET XACT_ABORT ON is recommended in all stored procedures with explicit transactions (unless you have a specific reason to do otherwise) as the consequences of an application performing work on a connection with an open transaction are disastrous.

There's a really great overview on Dan Guzman's Blog,

Friday, June 4, 2021
 
Savageman
answered 7 Months ago
71

sh does not have arrays (or, rather, it has only one), and stream_parameters=(-ice_name "$icecast_show" -f mp3) is a syntax error. If you want to use the one array that you have available, you can do something like

set -- -ice_name "$icecast_show" -f mp3   
icecast_setup="icecast://$icecast_user:$icecast_password@$icecast_server:$icecast_port$icecast_mount_url"
set -- "$@" -re -i - -c:a libmp3lame -ac 2 -ar 44100 -content_type audio/mpeg -loglevel quiet -b:a "$stream_bitrate" -legacy_icecast "$icecast_legacy" 


pipe_cmd "$@"  "$icecast_setup"
Saturday, August 7, 2021
 
Sidarta
answered 4 Months ago
59

Try

`bash -c "your command with args"`

I am fairly sure the argument of -c is interpreted the way bash interprets its command line. The trick is to protect it from sh - that's what quotes are for.

Saturday, August 14, 2021
 
jsmith
answered 4 Months ago
71

Angular supports lazy loading of modules. This way you can split up your application into parts that are loaded only on demand.

{
  path: 'admin',
  loadChildren: 'app/admin/admin.module#AdminModule',
},

See also https://angular.io/docs/ts/latest/guide/router.html#!#asynchronous-routing

update

While lazy loading doesn't reduce the load time when everything is required at the start page which is IMHO not the case in almost 100% of Angular2 applications, it reduces the time for the initial load and delays the load time to when a module is actually required. Angular also supports preload feature to load lazy loaded modules before they are required, but still doesn't load them with the initial load.

  • It will increase the number of request between server and client?

The number of requests will be higher, but the amount of data loaded with the first request will be less. The main idea is to reduce the time until the user gets the first screen rendered. If an applications consists of parts where some are heavily used and other rarely, then it's usually a good idea to not load the rarely used parts until they are actually used - which means "lazy" or "on demand" - only when actually required.

  • It will increase the complexity of your code.(Since you need to take care of

If you want to gain full benefit of lazy loading, you will need to think about your architecture. That doesn't mean the code needs to become more complex. You just need to make decisions which module you put your components, directives, services into.

  • importing and exporting modules and take special care in routing too)

I don't see much difference here, because you should split your application in different modules (for example per feature) anyway, even if they are not lazy loaded.

Wednesday, November 17, 2021
 
DreamsOfHummus
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share