Some questions about commands in bash script

Discussion in 'Programming/Scripts' started by vl72, Mar 5, 2011.

  1. vl72

    vl72 New Member

    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. falko

    falko Super Moderator

    Please try double quotes instead of single quotes:
    Code:
    var_2="File\ with\ some\ words\ in\ it.txt"
     

Share This Page