Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

    File Test Operators(bash script use කරල file එකක් හදල permission දීල run කරමු)

    Linux
    bash scripting linuxfoundation
    2
    2
    599
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • binurayeshan
      binurayeshan Linux Help last edited by

      හරි මම ඔයාලට කියල දෙන්නෙ මේ වෙනකන් කියල දීපු හැම පොස්ට් එකකින්ම ගත්ත දේවල් එකතු කරල කොහොමද linux වලදි එක ස්ක්‍රිප්ට් එකකින් ෆයිල් එකක් හදල ඒක ඇතුලෙ දෙයක් type කරල ඒකට execute permission දීලා ඒක run කරන්නෙ කියලා.

      හරි දැන් ඔයාල මෙන්න මේ වගේ sh file එකක් හදාගෙන script එක ලියන්න.

      #!/bin/bash
      
      cd ~ #මේ කියන්නෙ ඔයා ඉන්න directory එකෙන් ඊට කලින් තිබ්බ directory එකට යන්න කියල  මම මේ ඉන්නෙ Documents Directory එකේ තියෙන folder එකක ඒ නිසා මම මගේ User Directory එකට යන්න දෙපාරක් back වෙන්න ඕන  
      cd ~ # මේ දෙවනි එක 
      
      cd Desktop# හරි මම දැන් ඔන්න Script එකේ ලියනවා Desktop Directory එකට එන්න කියල 
      
      if [ -d new ] #මේ කියන්නේ new කියල Directory එකක් තියෙනවද කියල බලන්න කියන එක 
      then
      	echo "Directory available" #එහෙම තියෙනවනම් Directory available කියල display කරන්න
      	cd new #තියෙනවනම් ඒක ඇතුලට යන්න 
      else 
      	echo "Directory not available so i am creating file.."#එහෙම නැත්නම්not  Directory available කියල display කරන්න
      	mkdir new#Directory එක හදන්න
      	cd new#දැන් ඒක ඇතුලට යන්න
      fi #එක decision making command set එකක් ඉවරයි
      
      if [ -f foo.sh ] #හරි දැන් බලන්න foo.sh කියල file එකක් තියෙනවාද කියල 
      then 
      	echo "File available"#එහෙම තියෙනවානම් File available කියල print කරන්න 
      else
      	echo "File not available so i am creating file..." #එහෙම නැත්නම් File not available so i am creating file..." කියල print කරලා
      	touch "foo.sh"; echo "#!/bin/bash" >> foo.sh #File එක හදන්න හදලා ඒකෙ #!/bin/bash කියල type කරන්න කියන්න 
      fi
      
      if [ -e foo.sh ] #හරි මේ බලන්නෙ ඒ හදපු ෆයිල් එක ඇතුලෙ text මොනවා හරි තියෙනවාද කියල
      then 
      	echo "available"# එහෙම තියෙනවානම් available කියල print කරන්න 
      else
      	echo "Not available so i am writing.." #එහෙම නැත්නම් 
      	echo "#!/bin/bash" >> foo.sh##!/bin/bash කියල ෆයිල් එක ඇතුලෙ ටයිප් කරන්න 
      fi
      
      if [ -x foo.sh ]#මේ කියන්නෙ ඒ හදපු ෆයිල් එකට execution permission දීලද බල්න්න කියල 
      then
      	echo "Yes executable file so i am opening file"#එහෙම දීලනම්
      	open foo.sh#open කරන්න 
      	./foo.sh#Execute කරන්න 
      else
      	echo "Not a executable file i give permission and opening file"#file එකට permission දීල නැත්නම් 
      	chmod +x foo.sh #permission දෙන්න 
      	open foo.sh
      	./foo.sh
      fi
      
      echo "enter yes if you want edit file otherwise enter no" # හරි මම දැන් කියනවා ඔයාට ඒ file එක ආයිත් edit කරන්න ඕනනම් yes කියල type කරන්න එපානම් No කියල ටයිප් කරන්න 
      read input #user ගේ input එක ගන්න 
      
      if [ "$input" = "yes" ] # input එක yes නම් 
      then
      	open foo.sh # ෆයිල් එක open කරන්න 
      else 
      	echo "Work done bye!!!!" 
      fi
      
      

      :alien:

      0_1570411209941_Screen Shot 2019-10-07 at 6.50.04 AM.png

      1 Reply Last reply Reply Quote 3
      • Nubelle
        Nubelle Web Development last edited by

        eka patta mchan

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post

        0
        Online

        3.7k
        Users

        1.3k
        Topics

        5.3k
        Posts

        • Privacy
        • Terms & Conditions
        • Donate

        © Copyrights and All right reserved Lanka Developers Community

        Powered by Axis Technologies (PVT) Ltd

        Made with in Sri Lanka

        | |