I know that this can be done using ls -R path. But I'm trying to learn the syntactic and control structures of the shell language, so I'm trying to write my own code:
ls -R path
#!/bin/sh arg=$1; lsRec() { for x in $1*; do if [ -d "$x" ]; then lsRec $x; else echo "$x"; fi done } lsRec $arg;
When I call the command ./ej2.sh ~/Documents/, the terminal provides: segmentation fault (core dumped). Why am I getting this error? Am I missing something in my code?
./ej2.sh ~/Documents/
segmentation fault (core dumped)
Thank.
, lsRec , "/" . , , "/" , , , , "/" . , , , , lsRec $x/ ( ) , for x in $1/*; do ( ).
lsRec
lsRec $x/
for x in $1/*; do
, (, for x in "$1/"*, lsRec "$x", lsRec "$arg"), , . , , .
for x in "$1/"*
lsRec "$x"
lsRec "$arg"
, " x $1 *" $1, ? , . :
$1 , ? , "" , " ". "" , , .
, "" "hello/*" "hello *".
:
#!/bin/sh arg=$1; lsRec() { for x in "$1"/*; do echo "$x" if [ -d "$x" ]; then echo "This is a directory:" lsRec "$x"; else echo "This is not a directory:" echo "$x"; fi done } lsRec "$arg";
, !
I think you created a bomb plug. Your code creates infinite recursion.
You must change the code to:
#!/bin/sh arg=$1; lsRec() { for x in $1/*; do if [ -d "$x" ]; then echo "$x" ## here you print the line lsRec $x; ## here you pass the contents and NOT the line itself ## your programm was passing dirs without processing ## over and over else echo "$x"; fi done } lsRec $arg;
Source: https://habr.com/ru/post/1651222/More articles:Authenticate ASP forms with WCF? - authenticationHow to access any of the classes / methods from another Rails application? - ruby | fooobar.comTypescript/D3 v4 - Контекст этого в d3.drag(). on ( "end", this.dragended) - typescriptGrouping faces with equal rows - rAppDelegate.m for FBSDK and LinkingManager - iosHow can I make a rounded image with a label in it? - jqueryXS: Passing Perl XS External Library Callback Function - chtaccess Переписать URL-адрес на основе TLD - redirectmapstruct and gradle configuration problem in Intellij IDEA - intellij-ideabootstrap affix how to set it to parent element - javascriptAll Articles