argv

BASH scripting: n-th parameter of $@ when the index is a variable?

ぐ巨炮叔叔 提交于 2021-02-18 04:47:06
问题 I want to retrieve the n-th parameter of $@ (the list of command line parameters passed to the script), where n is stored in a variable. I tried ${$n}. For example, I want to get the 2nd command line parameter of an invocation: ./my_script.sh alpha beta gamma And the index should not be explicit but stored in a variable n. Sourcecode: n=2 echo ${$n} I would expect the output to be "beta", but I get the error: ./my_script.sh: line 2: ${$n}: bad substitution What am I doing wrong? 回答1: Try this

change working directory from csh script

霸气de小男生 提交于 2021-01-28 21:43:40
问题 I want to lookup some data of running processes, actually I'm searching for the scratch directory to a corresponding job-ID. I managed to do that manually by the following commands (assuming the job-ID is 12345): find ~ -name '12345.out' This finds the output file according to the job: /home/username/somefolder/12345.out The scratch directory is written to a file named .scrdir in that folder, so cd | cat /home/username/somefolder/.scrdir brings me where I want. I now want to combine that to a

Can I pass a whole string as one command line argument into argv in C?

好久不见. 提交于 2020-12-12 14:19:29
问题 Say I have a program like this: int main(int argc, char **argv){ printf("The name of the program is %s, and the string passed is:\n %s \n\n", argv[0], argv[1]); return 0; } I realize there should be error checking and whatnot, but what I'd really like to know is how to pass a whole string as the first argument or if it's even possible. Thanks! 回答1: Just use quotes ./file_name "This is the long string argument that comes in argv[1]" | | +--- argv[0] Edit : aruisdante: single and double quotes

Can I pass a whole string as one command line argument into argv in C?

此生再无相见时 提交于 2020-12-12 14:18:11
问题 Say I have a program like this: int main(int argc, char **argv){ printf("The name of the program is %s, and the string passed is:\n %s \n\n", argv[0], argv[1]); return 0; } I realize there should be error checking and whatnot, but what I'd really like to know is how to pass a whole string as the first argument or if it's even possible. Thanks! 回答1: Just use quotes ./file_name "This is the long string argument that comes in argv[1]" | | +--- argv[0] Edit : aruisdante: single and double quotes

Can I pass a whole string as one command line argument into argv in C?

♀尐吖头ヾ 提交于 2020-12-12 14:15:19
问题 Say I have a program like this: int main(int argc, char **argv){ printf("The name of the program is %s, and the string passed is:\n %s \n\n", argv[0], argv[1]); return 0; } I realize there should be error checking and whatnot, but what I'd really like to know is how to pass a whole string as the first argument or if it's even possible. Thanks! 回答1: Just use quotes ./file_name "This is the long string argument that comes in argv[1]" | | +--- argv[0] Edit : aruisdante: single and double quotes

How to read from files in argv or stdin if none are given?

一世执手 提交于 2020-08-17 11:06:30
问题 I have a program that calculates a lottery tickets (this tickets are in a file.txt), and writes the winners tickets in another file. I have a subfunction called evaluate_tickets(file, lottery_numers, winner....) In shell I write: ./program arg1 arg2... (arg1, arg2 are text files i.e. file.txt) But now, I want to do ./program < file.txt . The problem is that I don't know how to send the parameter "file" of evaluate_tickets because I receive information by stdin. 回答1: Define a stream pointer

Initialize/set char *argv[] inside main() in one line

回眸只為那壹抹淺笑 提交于 2020-05-12 03:45:06
问题 I want to initialize/set char *argv[] inside the main() so that I can use argv[1], argv[2]... later in my program. Up to now, I know how to do this in two ways: For int main() , use one line as: int main() { char *argv[] = {"programName", "para1", "para2", "para3", NULL}; } Note that, using NULL in the end is because the pointers in the argv array point to C strings, which are by definition NULL terminated. For int main(int argc, char* argv[]) , I have to use multiple lines as: int main(int

Initialize/set char *argv[] inside main() in one line

孤人 提交于 2020-05-12 03:44:05
问题 I want to initialize/set char *argv[] inside the main() so that I can use argv[1], argv[2]... later in my program. Up to now, I know how to do this in two ways: For int main() , use one line as: int main() { char *argv[] = {"programName", "para1", "para2", "para3", NULL}; } Note that, using NULL in the end is because the pointers in the argv array point to C strings, which are by definition NULL terminated. For int main(int argc, char* argv[]) , I have to use multiple lines as: int main(int

create array of pointers to files

那年仲夏 提交于 2020-04-08 06:40:21
问题 How would I go about making an array of file pointers in C? I would like to create an array of file pointers to the arguments of main... like a1.txt, a2.txt, etc... So I would run ./prog arg1.txt arg2.txt arg3.txt to have the program use these files. Then the argument for main is char **argv From argv, I would like to create the array of files/file pointers. This is what I have so far. FILE *inputFiles[argc - 1]; int i; for (i = 1; i < argc; i++) inputFiles[i] = fopen(argv[i], "r"); 回答1: The

replacing the command line arguments int argc and char** argv with std::vector<std::string>

三世轮回 提交于 2020-03-14 18:37:48
问题 Following this post, where I have found a temporary workaround for my other problem, I want to know if I can replace the int argc, char** argv with a std::vector<std::string> variable/object. Consider the imaginary code: #include <iostream> #include <CloseLibrary> void someFunction(int argc, char** argv){ for (int i = 0; i < argc; ++i) { std::cout << argv[i] << std::endl; } } int myFunc(int argc, char** argv){ someFunction(argc, argv); return 0; } where the CloseLibrary is a closed library