Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginGeneral Tech Bugs & Fixes 3 years ago
User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
Your existing code tests for the existence of a file called *.csv (literally). This is why it does not work. It does that because the * is within double quotes.
Using [ -f *.csv ] will not work as expected if there are multiple files matching the pattern. If there are multiple files matching *.csv, the cp command will additionally not work properly, unless listing.txt is the name of a directory.
To rename the first of the files matching the filename globbing pattern *.cvs in the current directory, do this:
#!/bin/sh
set -- *.csv
while [ "$#" -gt 0 ] && [ ! -f "$1" ]; do
shift
done
if [ -f "$1" ]; then
mv -- "$1" listing.txt
fi
This first sets the positional parameters, $1, $2, etc., the all names matching *.csv in the current directory. Then it starts looking for the first regular file, or symbolic link to a regular file, among the matched names (the code assumes that the *.csv glob may expand to directory names etc.). If a regular file, or a symbolic link to one, is found, it renames that file to listing.txt.
In the zsh shell:
mv -- *.csv(-.[1]) listing.txt
This uses a glob modifier for the *.csv globbing pattern that returns the first regular file or symbolic link that matches the pattern.
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
manpreet
Best Answer
3 years ago
I am trying to write a script that starts off with f="https://forum.tuteehub.com/tag/testing">testing for the f="https://forum.tuteehub.com/tag/existence">existence of a CSV file. If it finds one, rename it to listing.txt.
This is what I have but it doesn't work.