This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
hi im new to linux and need help with a shell script i am writing to automatically find the ip of ftp servers and copy files to them. this is the code i have but it errors when run
USER=anonymous
PWD=any
PORTCHECK="1"
for IP in 'sqe 6 250';
do
PORTCHECK= nmap -p21 -oG - 192.168.1.$IP | grep '21/open'
if [$PORTCHECK != "1" ]; then
ftp -p -n -i -v 192.168.1.$IP
user $USER $PWD
cd "Mounted Volume"
put foo
put bar
bye
if
CHECKPORT="1"
done
and the error is
./newscrpt: line 19: syntax error near unexpected token `done'
./newscrpt: line 19: `done'
this is being used to copy file to multiple winCE media system after reinstall please help i cant work it out
You meant to say fi instead of if just above CHECKPORT I think.
If you look at the structure of if statements you will notice the that they need a then and fi. See bellow:
if [ conditional expression ]
then
//stuff
fi
Instead of writing for IP in 'sqe 6 250'; (broken code with seq), use :
for IP in {6..250}
Related
So basically i have this issue with my Easter calculator, its a bash script that i have checked on shellcheck but with little to no luck, this is the code :
#!/shell/bash
read -r -p year
Am19=$((year% 19))
m19=$((19*(Am19)))
Am4=$((year%7))
m4=$((19*(Am4)))
Am2=$((year%4))
m2=$((2*(Am2)))
Av2=$((16+(m19)))
v2=$((Av2%30))
Av1=$((6*(v2)+m4+m2))
v1=$((Av1%7))
p=$((v1+v2))
echo "$p"
Everything appears fine but every time i input a number it always comes out with the result 21
I cant spot any issue, any help would be highly appreciated.
Very interesting issue, especially since shellcheck didn't point out the problem. The problem is in the first line
read -r -p year
Here year is not a variable name but a prompt (a literal string to be printed) given to -p. From help read (shortened version):
read [-r] [-p prompt] [name ...]
If no NAMEs are supplied, the line read is stored in the REPLY variable.
The string entered by the user is stored in the variable REPLY and not in year. The variable year remains unset and will expand to 0 in an arithmetic context like ((…)).
Use read -r -p year year to fix the problem, or even better
read -r -p 'enter a year: ' year
Further improvements
Is #! /shell/bash really correct? I never heard of a system using such a path. I would expect /bin/bash.
You can do assignments inside ((…)), there is no need for a subshell. Write ((a=b+c)) instead of a=$((b+c)).
Thank you for your concern.
I'm a noob trying to bulk-ip-lookup with [geoiplookup -f GeoLiteCity.dat] command.
I have more than 700 ips to lookup which saved on as c.txt (on the same folder)
How can I make a bash shell script? I've already made one and all I got was:
sudo: unable to execute ./ok.sh: No such file or directory
Here is my script
It would be all - ok to use another language.
To make it more clear;
[geoiplookup -f GeoLiteCity.dat IP1]
[geoiplookup -f GeoLiteCity.dat IP2]
...
[geoiplookup -f GeoLiteCity.dat IP700]
and save them as one text file. (Which would be 700 row)
I'm Korean and sorry for my poor English, but I couldn't find any in my language how to do this. I'll really appreciate it, or I have to look up 1 by 1 till Friday... (as internet speed is extremely slow in my company)
Please help me. I will pray for you at every sunday morning. Thank you.
found a very simple answer with a duckduckgo search for 'iterate through each line of file bash'
stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash
#!/usr/bin/bash
printf "\n\n"
while read ip; do
echo "LOOKING UP IP $ip"
geoiplookup $ip
printf "\n\n"
done < ipaddresses.txt
save it as iplookup.sh and run, without 'sudo':
bash iplookup.sh
tested and working. be sure to rename your file 'c.txt' to 'ipaddresses.txt' ! also the 'ipaddresses.txt' file must be in the same directory
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am attempting a simple file existence check in my bash script:
FILE_TO_CHECK=/home/username/path/to/file
if [ ! -f $FILE_TO_CHECK]; #line 9 in actual script
then
echo File not found.
fi
Seems simple enough to me, but I'm getting the following error and I don't know why:
/path/to/script: line 9: [: missing `]'
I'm not exactly a bash expert, but I was pretty sure a backtick is not necessary in this context. What gives?
Missing space before the closing ].
You have to understand that [ is a command and everything following it, until the ;, are its arguments. Command [ expects its last argument to be ]. But if you omit the space, then ] becomes the last character of the previous argument.
It might seem that [ ] is part of the if syntax. It's not the case. if has to be followed by any command, and if evaluates its exit status.
if true; then foo; fi
In the above line true is a command too.
$ which true
/bin/true
true is a command with the sole purpose of always having a true (0) exit status.
You could also try:
if (test ! -f $FILE_TO_CHECK);
or
if !(test -f $FILE_TO_CHECK);
as [ is a shorthand for the test command.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Right now I'm stuck with this project in having to pull information from an excel file to create users. The script requires the following.
Create accounts for the employees
Place them into his or her own department group
Set an initial password using his or her employee ID
Send an email containing their new account and password
show progress with dots on the screen
I'm getting close but am getting EOF errors on line 8 and 13 see script here
#!bin/bash
echo $(pwd)/employeesdata.xls
Path=$($pwd)/employeesdata.xls
read Path
if [ -e $Path ];
then
Username= [ grep $(Username) $Path | cut -f1 -d `]'
Password= [ grep $(Password) $Path | cut -f2 -d `]'
useradd -- b $Username
echo $Password | /usr/bin/passwd --stdin $Username
fi
exit
I know it doesn't have the email portion yet.
PS-UserScript: line 8: unexpected EOF while looking for matching `''
PS-UserScript: line 13: syntax error: unexpected end of file
You can use Spreadsheet::ParseExcel for this task:
First make sure cpanm is installed:
$ cpan App::cpanminus
Then install the module:
$ cpanm Spreadsheet::ParseExcel
Once installed, you will be able to use the parsing script that you have copied into your question. That script will fetch each cell one at a time, but you'll still need to do something with those values once read. Depending on how the spreadsheet is setup (if you provide a sample or at least the headers that would be helpful), you would want to gather the info you need as you iterate over the rows, and then perform the desired actions using the data you collected.
Also, I want to point out that the file you parse has to be in the older *.xls format. ParseExcel will not parse *.xlsx files, so first save in the older format using Excel if required.
This simple python script iterate over each cells/rows, that will be a good start I think :
#!/usr/bin/python
from openpyxl import load_workbook
wb = load_workbook(filename = 'file.xlsx', use_iterators = True)
ws = wb.get_sheet_by_name(name = 'Feuil1') # ws is now an IterableWorksheet
for row in ws.iter_rows(): # it brings a new method: iter_rows()
for cell in row:
print cell.internal_value
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I haven't got Linux on my computer at the moment, so I was wondering if someone can test this code I wrote.
It is supposed to rename a file extension when you type something like this, to run it, into the terminal:
chaxxx zzz yyy *.zzz
"chaxxx" being the name of the file.
Here's the code I wrote:
>>deleted<<
Use an online compiler & interpreter for your tests. ideone supports Bash Script too.
EDIT:
It does work. ren.sh is your script name, here you go:
$ ls
asdf.doc ren.sh text.txt
$ ./ren.sh txt doc *.txt
text.txt
text
$ ls
asdf.doc ren.sh text.doc
Have you looked at the rename command? You are pretty much reinventing the wheel here.
From man rename
rename .htm .html *.htm
will fix the extension of your html files.
Edit
If you are going to do it yourself in bash then I would suggest the following code instead. Here are its benefits:
It handles files with spaces in
their names
It checks to see if the file it's about to modify actually
ends in the extension you want to
change before it attempts to mv
it.
It uses native Parameter Expansion syntax rather than call the external binary basename
It checks to see if the # of input parameters is at least 3, otherwise it echos a usage message and exits
It uses a for-loop with indirection rather than calling the test with shift
#!/bin/bash
if (( $# < 3 )); then
echo "Usage: $0 oldExt newExt files"
exit
fi
EXTf=$1
EXTt=$2
for (( i = 3; i <= $#; i++)); do
NAME=${!i}
if [[ "${NAME##*.}" == "$EXTf" ]]; then
mv "$NAME" "${NAME%.*}.$EXTt"
fi
done