Passing a path as an argument to a shell script

12,906

Your code and your question are a bit messy and unclear.

It seems that you intended to find your file, given as a parameter to your script, but failed due to the maxdepth.

If you are given next/123/file.txt as an argument, your find gives you a warning:

find: warning: you have specified the -maxdepth option after a non-option argument -iname, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

Also -maxdepth gives you the depth find will go to find your file until it quits. next/123/file.txt has a depth of 2 directories.

Also you are trying to copy the given file within find, but also copied it using cp afterwards.

As said, your code is really messy and I don't know what you are trying to do. I will gladly help, if you could elaborate :).

There are some questions that are open:

  1. Why do you have to find the file, if you already know its path? Do you always have the whole path given as an argument? Or only part of the path? Only the basename ?
  2. Do you simply want to copy a file to another location?
  3. What does your writefile.c do? Does it write the content of your file to another? cp does that already.

I also recommend using variables with CAPITALIZED letters and checking the exit status of used commands like cp and find, to check if these failed.

Anyway, here is my script that might help you:

#!/bin/sh
#FIRST SCRIPT
clear
echo "-----STARTING COMPILATION-----"
echo "FILE: $1"
[ $# -ne 1 ] && echo "Usage: $0 <file>" 1>&2 && exit 1

FILE="$1"   # Copy the filename to name
FILE_NEW="tempwithfile.adb"
cp "$FILE" "$FILE_NEW" # Copy the file to new_file
[ $? -ne 0 ] && exit 2

echo
echo "----[ COMPILING ]----"
echo
dir &> filelist.txt # list directory contents and write to filelist.txt
gcc writefile.c # ???

FILE_RUN="run_file.txt"
echo "$FILE" > "$FILE_RUN"

./a.out

echo
echo "----[ CLEANING ]----"
echo

make clean
make -f makefile
./semantizer -da < withfile.adb
Share:
12,906
Jackzz
Author by

Jackzz

A simple software developer working with C++..

Updated on June 04, 2022

Comments

  • Jackzz
    Jackzz almost 2 years

    I've written bash script to open a file passed as an argument and write it into another file. But my script will work properly only if the file is in the current directory. Now I need to open and write the file that is not in the current directory also.

    If compile is the name of my script, then ./compile next/123/file.txt should open the file.txt in the passed path. How can I do it?

    #!/bin/sh
    #FIRST SCRIPT
    clear
    echo "-----STARTING COMPILATION-----"
    #echo $1
    name=$1   # Copy the filename to name
    find . -iname $name -maxdepth 1 -exec cp {} $name \;
    new_file="tempwithfile.adb"
    cp $name $new_file #copy the file to new_file
    
    echo "compiling"
    dir >filelist.txt
    gcc writefile.c
    run_file="run_file.txt"
    echo $name > $run_file
    ./a.out
    echo ""
    echo "cleaning"
    echo ""
    
    make clean
    make -f makefile
    ./semantizer -da <withfile.adb