shell - setting variable Linux -
i new linux shell scripting , trying set following variable size of folder path passed in keyboard. keep getting error saying invalid option. t
can tell me doing wrong? error disappears when remove "size" variable , leave is.
thanks!
#!/bin/bash echo -e "\nenter first directory (relative path)" read dir1 size=find $dir1 -type f | wc -l echo $size
it should be:
size=$(find $dir1 -type f | wc -l) using $() notation means command should run , output placed on line.
this because variables assigned literal text values.
myvar=foo will put string "foo" myvar , not value of variable foo or result of foo command.
myvar=$foo will put value of variable foo myvar. and
myvar=$(foo) will put result of running command foo myvar.
there special part of shell syntax lets assign variable values before running command, , variable values set 1 command. looks this:
x=foo1 y=foo2 command parameter ... the way have written line
size=find $dir1 -type f | wc -l size assigned value of "find", then
$dir1 -type f # won't valid command is executed separate command.
Comments
Post a Comment