简体   繁体   中英

I can't get my bash script to run

This is the script that I used to that will not run, but I am hoping someone can help me figure out what the issue is. I am new to unix

#!/bin/bash

# cat copyit

# copies files


numofargs=$#
listoffiles=
listofcopy=


# Capture all of the arguments passed to the command, store all of the                     arguments, except
# for the last (the destination)


while [ "$#" -gt 1 ]
do
listoffiles="$listoffiles $1"
shift
done

destination="$1"


# If there are less than two arguments that are entered, or if there are          more than two
# arguments, and the last argument is not a valid directory, then display an
# error message


if [ "$numofargs" -lt 2 -o "$numofargs" -gt 2 -a ! -d "$destination" ]
then
echo "Usage: copyit sourcefile destinationfile"
echo" copyit sourcefile(s) directory"
exit 1
fi


# look at each sourcefile


for fromfile in $listoffiles
do


# see if destination file is a directory


if [ -d "$destination" ]
then
destfile="$destination/`basename $fromfile`"
else
destfile="$destination"
fi


# Add the file to the copy list if the file does not already exist, or it
# the user
# says that the file can be overwritten



if [ -f "$destfile" ]
then 
echo "$destfile already exist; overwrite it? (yes/no)? \c"
read ans

if [ "$ans" = yes ]
then
listofcopy="$listofcopy $fromfile"
fi

else
listofcopy="$listofcopy $fromfile"
fi
done


# If there is something to copy - copy it


if [ -n "$listofcopy" ]
then
mv $listofcopy $destination
fi

This is what I got and it seems that the script didn't execute all though I did invoke it. I am hoping that someone can help me

[taniamack@localhost ~]$ chmod 555 tryto.txt
[taniamack@localhost ~]$ tryto.txt
bash: tryto.txt: command not found...
[taniamack@localhost ~]$ ./tryto.txt
./tryto.txt: line 7: $'\r': command not found
./tryto.txt: line 11: $'\r': command not found
./tryto.txt: line 16: $'\r': command not found
./tryto.txt: line 43: syntax error near unexpected token `$'do\r''
'/tryto.txt: line 43: `do

Looks like your file contains Windows new line formatting: "\\r\\n". On Unix, a new line is just "\\n". You can use dos2unix ( apt-get install dos2unix ), to convert your files.

Also have a look at the chmod manual (man chmod). Most of the time i just use chmod +x ./my_file to give execution rights

I see a few issues. First of all, a mode of 555 means that no one can write to the file. You probably want chmod 755 . Second of all, you need to add the current directory to your $PATH variable. In Windows, you also have a %PATH% , but by default the current directory . is always in %PATH% , but in Unix, adding the current directory is highly discouraged because of security concerns. The standard is to put your scripts under the $HOME/bin directory and make that directory the last entry in your $PATH .

First of all: Indent correctly. When you enter a loop or an if statement, indent the lines by four characters (that's the standard). It makes it much easier to read your program.

Another issue is your line endings. It looks like some of the lines have a Windows line ending on them while most others have a Unix/Linux/Mac line ending. Windows ends each line with two characters - Carriage Return and Linefeed while Unix/Linux/Mac end each line with just a Linefeed . The \\r is used to represent the Carriage Return character. Use a program editor like vim or gedit . A good program editor will make sure that your line endings are consistent and correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM