Find the answer to your Linux question:
Results 1 to 6 of 6
A)) write a shell script that will 1-prompt the user for a file name 2-check to see if the file exists 3-create the file if the file does not exist ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    8

    would anyone check this script if its right or wrong


    A)) write a shell script that will

    1-prompt the user for a file name
    2-check to see if the file exists
    3-create the file if the file does not exist
    4-then go back and repeat step 1 if the file doesn't exist


    #!/bin/bash
    for i in filename
    do
    echo -n "enter a file name: "
    read filename
    if [ -f "filename" ]; then
    echo "file is exist."
    else
    echo cat > “filename”
    fi
    done

    ========================================

    B)- create a shell script that will

    1) Prompt for four file names
    2) Combine all of the data in the first three files into the fourth file


    cat > file1
    cat > file2
    cat > file3
    cat > file4

    to combine …

    cat file1 file2 file3 > file4

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by flyman View Post
    A)) write a shell script that will

    1-prompt the user for a file name
    2-check to see if the file exists
    3-create the file if the file does not exist
    4-then go back and repeat step 1 if the file doesn't exist


    #!/bin/bash
    for i in filename
    do
    echo -n "enter a file name: "
    read filename
    if [ -f "filename" ]; then
    echo "file is exist."
    else
    echo cat > “filename”
    fi
    done

    ========================================
    This is almost right, but you've got some syntax problems, and you don't need the loop, e.g.:
    Code:
    #!/bin/bash
    read -p "enter a file name: " filename
    if [ -f "$filename" ]; then 
      echo "file $filename exists."
    else
      touch $filename
    fi
    Note, it just creates an empty file, if it didn't exist - is that what you really want?

    B)- create a shell script that will

    1) Prompt for four file names
    2) Combine all of the data in the first three files into the fourth file


    cat > file1
    cat > file2
    cat > file3
    cat > file4

    to combine …

    cat file1 file2 file3 > file4
    for this one, just loop the first code snippet 4 times and then do your cat command at the end, e.g.
    Code:
    for i in `seq 1 4`; do
      (code above to get filename1 ... filename4)
    done
    cat $filename1 $filename2 $filename3 > $filename4

  3. #3
    Just Joined!
    Join Date
    Aug 2011
    Posts
    8
    thank you soo much for the response .... but for the second question the one with combine files is my code wrong

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Your "cat > file1" code is not *wrong* - it depends on what you are doing. I guess you want to take STDIN (input typed by the user) and write that to a file named "file1", yes?

    I thought you were trying to get the actual name of the file from the user.

  6. #5
    Just Joined!
    Join Date
    Aug 2011
    Posts
    8
    for this one, just loop the first code snippet 4 times and then do your cat command at the end, e.g.
    Code:
    for i in `seq 1 4`; do
      (code above to get filename1 ... filename4)
    done
    cat $filename1 $filename2 $filename3 > $filename4
    [/QUOTE]

    getting this error

    ./sf: line 2: code: command not found
    ./sf: line 2: code: command not found
    ./sf: line 2: code: command not found
    ./sf: line 2: code: command not found
    ./sf: line 4: $filename4: ambiguous redirect

  7. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    That stuff in parenthesis was just pseudo-code; substitute the code from the first snippet. also, it would be better to make use of arrays, e.g.:
    Code:
    declare -a filenames
    for i in `seq 1 4`; do
      read -p "Enter file name #$i: " filename
      filenames[$i]=$filename
    done
    cat ${filenames[1]} ${filenames[2]} ${filenames[3]} > ${filenames[4]}
    That will take four filenames from user input, and concatenate the first three given into the fourth. Note that it does no checking on the filenames given, so you'll get errors if a fileaname is given that does not exist, or nothing is entered (they just hit Enter).

Posting Permissions

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