Find the answer to your Linux question:
Results 1 to 2 of 2
1) Tell me, please. How to work with files and folders in BASH-script if theirs names consist of some words? For example, name of file «File with some words in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2011
    Posts
    2

    Question Some questions about commands in bash script


    1) Tell me, please. How to work with files and folders in BASH-script if theirs names consist of some words?
    For example, name of file «File with some words in it.txt», name of folder «Folder where I saved my pictures»
    When I try execute this script and file «File with some words in it.txt» doesn't exist in my home folder I receive next errors:
    Code:
    #!/bin/bash
    cd $HOME
    var_1='File with some words in it.txt'
    var_2='File\ with\ some\ words\ in\ it.txt'
    
    if [ `ls $var_2 | wc -l` -gt 0 ]    
        then
    	    echo 'Yes!'
        else
    	    echo 'No!'
    fi
    ls: cannot access File\: No such file or directory
    ls: cannot access with\: No such file or directory
    ls: cannot access some\: No such file or directory
    ls: cannot access words\: No such file or directory
    ls: cannot access in\: No such file or directory
    ls: cannot access it.txt: No such file or directory

    2) How to create folder if its name consist of some words?
    For example, name of folder «Folder where I saved my pictures».

    3) What command can give me numeric position string character (word, phrase) that I have to search into another string from the start or from the end this string?
    For example, I search word «cat» in string «Black and white dogs and cats like to eat meat» (1)
    Numeric position word “cat” is 26 from the start string (1) and 21 from the end.

    4) What command can append data in a new row (line) at the end of a text file? I want to direct output from cycle into the same text file. For example, this text file must have 10 lines after 10 cycle iteration, one record per line.

  2. #2
    Linux Engineer rcgreen's Avatar
    Join Date
    May 2006
    Location
    the hills
    Posts
    1,134
    File names that have more than one word must be enclosed
    in quotes. "my recipe for spaghetti sauce.txt"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •