简体   繁体   中英

Can't creat an sh script

On my Ubuntu 18.04 terminal, I try this:

cd home/myusername/bin
cat -n > test
#!/bin/sh
echo "Hello World"
chmod 755 test

Then, when I try to run my script with sh , I get:

test: 1: test: 1: not found
test: 2: test: 2: not found

When I run it with bash , I get:

test: line 1: 1: command not found
test: line 2: 2: command not found

and when I run it like this ./test , I get:

test: line 1: 1: command not found
test: line 2: 2: command not found

I have made sure that sh is located in the bin directory and also the directory that I created test is located in the path variable but it will not do anything if I try to run it with its name.

I can't figure out what I am doing wrong.

cat -n places a line number in front of each line, as you can see if you do cat test to show the contents of the file you wrote.

The shell does not understand these line numbers and tries to run a program named 1: , 2: , etcetera.

Simply omit the -n .

vincent@idefix:/tmp$ cat << EOF > test
#!/bin/sh

echo "Hello World"
EOF

chmod +x /tmp/test

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