Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
1.
I want to copy the number of lines in a file to a variable , but the command
linecount='wc -l $FTP_dir/deletedata.list' is giving me an error. It says -l is invalid symbol
1a. Were you using the apostrophe, ' or the backtick, `? It's not easy to see the difference, especially with some fonts, but you'll not get the right behavior if you get the wrong one. There is an alternative syntax, which I prefer in part because it's not so common to make this mistake... instead of `command`, use $(command).
1b. wc -l filename will output the linecount and the filename. You can prevent this by passing the file on standard input:
Splitting up a line into fields can be done in several ways. If you just want one particular field, the cut command is useful. It can split by a delimiting character or by character ranges, e.g.
Another approach is to tell the shell's read function to split the line. The splitting is usually done on whitespace (spaces and tabs), but you can set the IFS variable. You can set this just before calling read, and it is only set for that operation:
As you can see, if you only provide 3 variables but there are more fields in the input line, the last variable gets all the rest of the line which is not assigned yet. If you provide more variables than there are fields, the extra variable will be assigned with empty strings.
See "man bash" for more details. The only caveat here is that the bash manual page is so huge that finding the correct section is a pain in the bum. If you have the same version of the manual page that I have, you might try searching for "-n nchars", which takes me to the correct section in one go.
In your script you seem to want to take the part after the last /, split it by the period character, and use fields 5-10 to make FILENAME. You can get the last part of a path using basename (which you actually do, but don't use it), then use cut to construct the fields 5-10 part. For example:
I used $(basename "$FILE") instead of `basename "$FILE"`. It does the same thing but it's less likely to be mis-copied (lots of people use the wrong quote - ' instead of `). It also has the advantage that you can use a $() inside of another $(), whereas it's not possible to nest `backtick execution`.
There's another way to do basename which is less readable, but slightly faster. basename is a separate program from the shell which means the OS has to execute a new process, get the output, then close the process etc. All this takes some time. Linux us actually remarkably fast at this compared to other OSes, but even so, if you can do the same thing "inside" the shell process, it'll be lighter on your machine. The only dis-advantage is that the in-shell version is a bit less readable. Here it is anyhow ${FILE##*/}. Read the Parameter Expansion section of the bash manual page to understand this.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.